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 http://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:
http://www.mysite.com/home/about
You would preferably want it to be http://www.mysite.com/about
Anyone have a good enough understanding of the maproute object to help me understand this?
Thanks,
Jacques
This is fully customizable in the
global.asax.csBy default, a new MVC project adds the standard ‘catchall’ route which follows the convention:
www.mysite.com/Controller/Action/IDIf you want something different, such as your example of http://www.mysite.com/about routing to HomeController.About() action, you would add the following route before the catchall one:
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:
2.) You prefer to group by the type of objects for the Controller, but have the url’s be a verb/noun format:
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: