Routing in ASP.NET MVC

Bharathi Cherukuri
Posted by in ASP.NET MVC category on for Beginner level | Points: 250 | Views : 3865 red flag
Rating: 3 out of 5  
 2 vote(s)

In this article I am going to explain about Routing in ASP.NET MVC

Introduction

In this article we will see about MVC Routing.As we all know that Routing is one of the features of MVC.In this article we will explore each aspect of Routing.

Objective

MVC will create a control over how URLs are mapped to your controllers. It also gives you the ability to define your own URLs. These URLs will be in a readable friendly fashion as SEO (Search Engine Optimization). This will be helpful to remap old URLs to new functionality and side-by-side utilizes classic ASP.NET sites inside of MVC. The important note is that Routing is not URL rewriting, it will customize many attachments towards request/response.

Using the code

A Global.asax file is created when we create any type of MVC application by default. This is because, by using this global application class,  ASP.NET will implements MVC. Routes will be defined in the Global.asax.cs file of our MVC web application. The RegisterRoutes method is the main element in this global.asax file. 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 (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. The order is important when you add additional routes.

MVC Custom Routes

The search engines will consider mainly the factors 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, you might have a URL that looks like www.dotnetfunda.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.dotnetfunda.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, I have explained about the URL Routing as per my knowledge. By this article, I think you are clear about routing can perform the routing in your MVC application.

 

Page copy protected against web site content infringement by Copyscape

About the Author

Bharathi Cherukuri
Full Name: Bharathi Cherukuri
Member Level: Silver
Member Status: Member,MVP
Member Since: 1/30/2012 4:18:53 AM
Country: India

http://www.dotnetfunda.com

Login to vote for this post.

Comments or Responses

Posted by: Sandy's on: 8/28/2012 | Points: 25
Keep it up... My 4! :)

Login to post response

Comment using Facebook(Author doesn't get notification)