Routing in MVC


Introduction

In the MVC series we saw 3 articles which describe the MVC, Asp.Net MVC, Difference between MVC and Asp.Net MVC and Asp.Net MVC app execution. In this article we will see the MVC routing which I mentioned in the last article. Routing or Route Mapping is the feature of MVC. In this article we will explore each aspect of routing.

MVC gives you great control over how URLs are mapped to your controllers. It gives you the ability to define your URLs in a human readable SEO (Search Engine Optimization) friendly fashion, to remap old URLs to new functionality and side-by-side utilizes classic ASP.NET sites inside of MVC. It also results in hiding what kind of page a user is calling and what environment we are working in. Most of the new websites are following this and it is important to understand that routing is not URL rewriting as routing will have customizations and many attachments towards request/response.

When we create any type of MVC application by default Global.asax file is created because ASP.NET implements MVC using this global application class mainly. Routes defined in the Global.asax.cs file of our MVC web application. In this global.asax file most important element relative to our work is RegisterRoutes method. By default, there is only one route defined in the RegisterRoutes method that looks like the line below.

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 = UrlParameter.Optional } // Parameter defaults
    );

}

This route defines the route name, which can be anything as long as it is unique, the URL template, and the parameter defaults. The default route that is pre-defined for you maps to Controller/Action/id. You can add additional routes by copying the same line and adjusting the URL parameters and the related default values. Remember when we add additional routes the order is important.

MVC Custom Routes

One of the many factors frequently considered by search engines to determine the relevance of a particular page to a particular search term is whether or not the URL link itself includes a particular term. In a classic ASP.NET site for a magazine, you might have a URL that looks like www.c-sharpcorner.com/ViewArticle.aspx?id=123. This URL passes the ID number of the article to view, but the URL itself doesn't describe the content in any human readable way. If the URL instead was www.c-sharpcorner.com/MVC_Routing/123 a human-or a web crawler-could read that and know that the article is about MVC Routing.

Routes Re-Mapping

Sometimes it is necessary to map old urls to new location. In this case simply we use a redirect page to tell the user to update their bookmarks and add the link to new location. In some cases it is not easy or impossible also to access the particular URL.

If you want route to yourpage.aspx to your MVC Home controller and Index Action (Home/Index), you can do it by defining route like bellow.

routes.MapRoute ("RouteName","yourpage.aspx",new { controller = "Home", action = "Index", id = UrlParameter.Optional }

Conclusion

In this article we saw the main advantage of url routing of MVC. Using this description I think so you are clear about Routing can perform the routing in your MVC application.


Similar Articles