Sign In| Join
Get Help:Ask a Question in our Forums|Report a Bug|More Help Resources
Last post May 03, 2012 10:59 PM by Dobly
Member
254 Points
82 Posts
May 03, 2012 06:26 AM|LINK
I have a controller called CuisineController with one action method called Search. Looks like this. Simple stuff
public class CuisineController : Controller { public ActionResult Search() { return Content("In here"); } }
My routing looks like this..
public static void RegisterRoutes(RouteCollection routes) { routes.IgnoreRoute("{resource}.axd/{*pathInfo}"); routes.MapRoute( "Cuisine", "Cuisine/{name}", new { controller = "Cuisine", action = "Search" } ); routes.MapRoute( "Default", "{controller}/{action}/{id}", new { controller = "Home", action = "Index", id = UrlParameter.Optional } ); }
My problem is this.
/Cuisine/Search <- This works
However
/Cuisine <- 404 error
/cuisine/ <- 404 error
Does not work despite the fact I have a default action of 'Search'.
Seems the first MapRoute is failing and the 2nd one is picking it up. And seeings I don't have a Cuisine/Index action I get the error.
I'm a bit new to MVC so I must admit this has me stumped. Seems obvious how routing works but I can't work this out.
Cheers
Contributor
3230 Points
668 Posts
May 03, 2012 06:40 AM|LINK
change the highlighted line in your code for the second option in RegisterRoutes
routes.MapRoute(
"Cuisine",
"Cuisine/{name}",
new { controller = "Cuisine", action = "Search" ,name=UrlParameter.Optional }
);
May 03, 2012 10:59 PM|LINK
Oh, so simple when you know.. Thanks tusharrs.
Dobly
Member
254 Points
82 Posts
Simple routing problem
May 03, 2012 06:26 AM|LINK
I have a controller called CuisineController with one action method called Search. Looks like this. Simple stuff
public class CuisineController : Controller { public ActionResult Search() { return Content("In here"); } }My routing looks like this..
public static void RegisterRoutes(RouteCollection routes) { routes.IgnoreRoute("{resource}.axd/{*pathInfo}"); routes.MapRoute( "Cuisine", "Cuisine/{name}", new { controller = "Cuisine", action = "Search" } ); routes.MapRoute( "Default", "{controller}/{action}/{id}", new { controller = "Home", action = "Index", id = UrlParameter.Optional } ); }My problem is this.
/Cuisine/Search <- This works
However
/Cuisine <- 404 error
/cuisine/ <- 404 error
Does not work despite the fact I have a default action of 'Search'.
Seems the first MapRoute is failing and the 2nd one is picking it up. And seeings I don't have a Cuisine/Index action I get the error.
I'm a bit new to MVC so I must admit this has me stumped. Seems obvious how routing works but I can't work this out.
Cheers
tusharrs
Contributor
3230 Points
668 Posts
Re: Simple routing problem
May 03, 2012 06:40 AM|LINK
change the highlighted line in your code for the second option in RegisterRoutes
routes.MapRoute(
"Cuisine",
"Cuisine/{name}",
new { controller = "Cuisine", action = "Search" ,name=UrlParameter.Optional }
);
( Mark as Answer if it helps you out )
View my Blog
Dobly
Member
254 Points
82 Posts
Re: Simple routing problem
May 03, 2012 10:59 PM|LINK
Oh, so simple when you know.. Thanks tusharrs.