Reputation: 268
I am redesigning a site but one of the requirements I was given after starting the development with MVC5 is keeping the current URL structure intact.
The site is all designed but I am now looking for a way to use routes to build out these URLs.
I have seen a ton of posts and tutorials explaining the standard:
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
and some slight variations on it, like:
{controller}/{action}/{name}
But none to create the long folder dense structure I am trying to keep.
(Coded for formatting)
[http://www.domain.com/menu/.Dessert....../.Ice-Cream......./.Flavor-Ice-Cream/]
[http://www.domain.com/menu/{categoryName}/{subCategoryName}/{productInThisSubCategory}]
Some have a deeper folder structure, too.
I am sure there is an easy way to do this, but....
Upvotes: 0
Views: 73
Reputation: 47375
Use AttributeRoutes.
[HttpGet, Route("menu/{categoryName}/{subCategoryName}/{productName}")]
public ActionResult Menu(string categoryName, string subCategoryName,
string productName)
{
...
}
Attribute routes are new in MVC5, but are based on the popular AttributeRouting.NET tool that has been available for MVC4 and lower projects for some time now.
It allows you to keep the route definition closer to the action it works for, avoiding a "god route file" pattern for unconventional routes.
Upvotes: 1