1

Currently I'am working on an MVC project in which I try to get a kind of dynamic routing working. My idea would be that i left the original route in the global.asax.cs, so this one will take care of every controller I make. For example the Contact and Account controllers.

Above controllers will have url's like

/Contact/
/Account/Logoff/ etc.

The second route I want to add is the one that is a kind of default route when there are no controllers found. In that case I assume this will be a route to a page or pagedetails.

Url's for example will be :

/BBQ/
/BBQ/Accesoires/

I have three routes added in the global.asax.cs which I think are correct. (Also in the correct order). Below I have added the routes:

routes.MapRoute(
"DefaultRoute", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Page", action = "Index", id = UrlParameter.Optional });

routes.MapRoute(
"DefaultPageRoute",
"{category}",
new { controller = "Page", action = "Index", category = UrlParameter.Optional });

routes.MapRoute(
"SecondLevelPageRoute",
"{category}/{subCategory}",
new { controller = "Page", action = "PageDetails", category = UrlParameter.Optional, subCategory = UrlParameter.Optional });

with this setup the calls to the controllers work fine, but to the pages like /BBQ/ it gives below error:

Server Error in '/' Application.

The resource cannot be found.

If I comment the first route and go the the /BBQ/ url it works like a charm. What am I overseeing in this routetable?

jessegavin
  • 74,067
  • 28
  • 136
  • 164
ChristiaanV
  • 5,401
  • 3
  • 32
  • 42

1 Answers1

0

You put the default route first, so it is trying to go to a route defined by {controller = "BBQ", Action = "Index" }

That route should be the very last route. However, you need more detail in your routes. Just having a category route will cause problems.

For example, if this route is first

routes.MapRoute(
"DefaultPageRoute",
"{category}",
new { controller = "Page", action = "Index", category = UrlParameter.Optional });

Then a call to the URL /Contact/ will assume that you want to go to Page/Index/Contact not /Contact/Index/{id}. I would use a more specific route that signifies that you are browsing a category like:

routes.MapRoute(
"DefaultPageRoute",
"Category/{category}",
new { controller = "Page", action = "Index", category = UrlParameter.Optional });

So you will need to use a url www.mysite.com/Category/BBQ to view what you want, but I don't think that's all bad.

NerdFury
  • 18,876
  • 5
  • 38
  • 41
  • You're right, just had saw the light a few seconds ago too :). Would it be a bad idea to load my pagedetails from the database in the routing table? How will that affect performance? – ChristiaanV May 20 '11 at 18:26
  • I'm not sure. Seems like overkill, but if you want to avoid the 'Category' portion of the url, and add a route for each type, that would be the way to do it. The only way to know what the performance would be like is to try it. I've never done it myself. – NerdFury May 20 '11 at 18:38