0

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....

1 Answer 1

1

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.

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

Comments

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.