13.10. Custom Routing

By now, you should have a basic understanding of URL routing in MVC and how important it is. Routing is very flexible, and the default can be modified by changing the rules in Global.asax. Right-click Global.asax (it handles global events within your application), and select View Code. Your code will look similar to the following:

public static void RegisterRoutes(RouteCollection routes)
{
    routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

    routes.MapRoute(
        "Default",                                              // Route name
        "{controller}/{action}/{id}",                           // URL with parameters
        new { controller = "Home", action = "Index", id = "" }  // Parameter defaults
    );

}

protected void Application_Start()
{
    RegisterRoutes(RouteTable.Routes);
}

But what's this code doing? I will tackle the line routes.MapRoute first. This maps the default route the user will be sent to (in this case, ASP.NET MVC will send them to ~/home/index) if they do not enter a controller or action. Thus, if you want to alter the default view that ASP.NET MVC will serve up when the user goes to the root of your site, this is the place to do it.

The line routes.IgnoreRoute tells ASP.NET MVC to ignore any requests ending in the .axd extension, which is used by IIS to route incoming requests to HTTP modules and handlers. This is often done in order to generate something dynamically, such as a thumbnail image or chart. You don't want ASP.NET MVC to interfere with these types of requests, so this line tells ASP.NET MVC to ignore requests for any files ending in .axd.

You will want to create custom routes sooner or later. For example, in the movie theater example it would be useful if users could access information about a popular film by going to www.bobsMovies.com/kungFuPanda/ rather than something less memorable like www.bobsMovies.com/film/detail/2.

To implement this, all you need to do is add the route with the routes.MapRoute() method. Add the following route before the existing call to MapRoute():

routes.MapRoute(
    "PopularFilm",                                          // Route name
    "KungFuPanda",                                          // URL with parameters
    new { controller = "Film", action = "Detail", id = 1 }  // Parameter defaults
);

This code tells MVC that if it receives a request with KungFuPanda in it (such as www.bobsMovies.com/KungFuPanda) to send it to the Film controller's Detail action, passing in an ID of 1.

Check that this works by pressing F5 to run the application. Change the URL to ~/KungFuPanda/. You should be taken to the film detail view of film ID 1 (although the URL will appear as Film/KungFuPanda).

When you define routes, they are processed in the order in which they are declared. Make sure that you declare routes in order of specificity because a more general route could hide a specific route.


..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
52.15.80.101