1

I am trying to configure routing with both Conventional & Attribute based.

If I just use the default Conventional route included with MVC everything works. but if I add this Route attribute, I get a 404.

Here is the GET request URL: http://localhost:52386/Home/SimpleSearch?searchTerms=test&dateRange=0

Here is my RouteAttributes in Code:

[RoutePrefix("Home")] 
public class HomeController : Controller
{
    [Route("SimpleSearch/{searchTerms}/{dateRange}/{page?}")]
    [HttpGet]
    public ActionResult SimpleSearch(string searchTerms, DateRangeEnum dateRange, int page = 1)
    {
       //Code here
    }

}

Also the Route Config looks like this:

public class RouteConfig
{
    public static void RegisterRoutes(RouteCollection routes)
    {
        routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

        routes.MapMvcAttributeRoutes();

        //Default
        routes.MapRoute(
            name: "Default",
            url: "{controller}/{action}/{id}",
            defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
        );
    }
}

I don't see what is wrong with this RouteAttribute, but even if something is wrong with it, why doesnt it fall back onto the default Conventional Route and work?

1 Answer 1

1

With the attribute route definition, you explicitly specified the route pattern to be

Home/SimpleSearch/{searchTerms}/{dateRange}/{page?}

So you should try to access your action method with same url pattern.

This should work.

 http://localhost:52386/Home/SimpleSearch/test/0

and Model binder will be able to map "test" to searchTerms parameter and 0 to dateRange parameter.

Your conventional (explicitly using querystring) will not work when you have an attribute route with a different pattern

Sign up to request clarification or add additional context in comments.

8 Comments

This should work but it does not. It also gives me a 404 error.
The conventional routing does somehow work.. because when I remove the attribute routing the normal querystring get request works fine
It (the request with attribute route url pattern) should work (I just copied and pasted it and verified it). Conventional route wont work once you decorate it with attribute route definition (custom url pattern)
Why would the convential route no longer work? I am using both, they are both placed into the route dictionary because I declare both in the route config. Regardless I still cannot get it to work. the only way I works is If I make all the parameters in the route optional: [Route("SimpleSearch/{searchTerms?}/{dateRange?}/{page?}")]
I just tried the code from your quest and it worked fine for me (when i tried mysiteName/Home/SimpleSearch/test/0).
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.