1

I've read through a number of these questions about Asp.net MVC and directory structures, but I still don't understand how to immitate regular URL directories such as www.mysite.com/books/adventure/uk

And, when you think of standard site pages like Home, About, Contact Us, etc. You wouldn't want to create a Controller for each, but you also don't want to the Urls to look like this: www.mysite.com/home/about You would preferably want it to be www.mysite.com/about

Anyone have a good enough understanding of the maproute object to help me understand this?

Thanks, Jacques

1 Answer 1

1

This is fully customizable in the global.asax.cs

By default, a new MVC project adds the standard 'catchall' route which follows the convention: www.mysite.com/Controller/Action/ID

If you want something different, such as your example of www.mysite.com/about routing to HomeController.About() action, you would add the following route before the catchall one:

routes.MapRoute(
    "My About Route",
    "About",
    new { controller = "Home", action = "About" }
);

You can add any number of custom routes that don't follow the MVC catchall convention, and can even replace the catchall route altogether if you desire.


EDIT:

To answer your comment below, no you don't necessarily need to add a route for every possible action, instead you can make a single route that fits a pattern of url's - just like the catchall one is doing. {controller} and {action} are special keywords.

Examples:

1.) Program has a SetController, but user's are accustomed to calling them Batches:

routes.MapRoute(
    "Batch",
    "Batch/{action}/{id}",
    new { controller = "Set", action = "Index", id = UrlParameter.Optional }
);

2.) You prefer to group by the type of objects for the Controller, but have the url's be a verb/noun format:

  • FlightController
  • HotelController
  • /find/flight /book/flight
  • /find/hotel /reserve/hotel/Hilton

routes.MapRoute(
    "NewCatchall",
    "{action}/{controller}/{name}",
    new { controller = "Home", action = "Index", name = UrlParameter.Optional }
);

The second example can replace the default route altogether, and all possible url's would fit its format. So nothing would continue on to check any other routes. It's important to know that routes are checked in order, and the first pattern that matches the request is used - the rest ignored.

These were useful to me when learning:

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

2 Comments

thanks arserbin3. So what you're saying is that for any of these general pages you would have to create routes in the global asax file. I guess I'm just so used to working in CMS environments that it seems a bit overkill to have to do this everytime. It does make sense though from an MVC point of view. Thanks again
What about more detailed directory structures like www.mysite.com/services/electrical/contractors...?

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.