SIGN UP MEMBER LOGIN:    
ARTICLE

Routing in MVC

Posted by Krishna Garad Articles | ASP.NET MVC with C# January 12, 2012
In this article we will see the MVC routing which I mentioned in the last article.
Reader Level:

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.

Login to add your contents and source code to this article
share this article :
post comment
 

Thanks Stephen

Posted by Krishna Garad Jan 13, 2012

Its a very useful article for those who want to learn about MVC routing.

Posted by Stephen Johnson Jan 13, 2012
Become a Sponsor
PREMIUM SPONSORS
  • ceTE software specializes in components for dynamic PDF generation and manipulation. The DynamicPDF™ product line allows you to dynamically generate PDF documents, merge PDF documents and new content to existing PDF documents from within your applications.
    ceTE software specializes in components for dynamic PDF generation and manipulation. The DynamicPDF™ product line allows you to dynamically generate PDF documents, merge PDF documents and new content to existing PDF documents from within your applications. Visit DynamicPDF here
6 Months Free & No Setup Fees ASP.NET Hosting!
Become a Sponsor