Congratulations to all the winners of April 2013, they have won INR 3400 cash and INR 20147 worth prizes !
Go to DotNetFunda.com
Twitter TwitterLinkedIn
YouTubeGoogle
 Online : 23255 |  Welcome, Guest!   Register  Login
Home > Articles > ASP.NET MVC > Routing in ASP.NET MVC

Routing in ASP.NET MVC

2 vote(s)
Rating: 3 out of 5
Article posted by Bharathi Cherukuri on 8/23/2012 | Views: 2038 | Category: ASP.NET MVC | Level: Beginner | Points: 250 red flag


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.

 

If you like this article, subscribe to our RSS Feed. You can also subscribe via email to our Interview Questions, Codes and Forums section.

Page copy protected against web site content infringement by Copyscape
Found interesting? Add this to:



Please Sign In to vote for this post.

Latest Articles from Bharathi Cherukuri
Experience:0 year(s)
Home page:http://www.dotnetfunda.com
Member since:Monday, January 30, 2012
Level:Silver
Status: [Member]
Biography:
 Responses
Posted by: Sandy's | Posted on: 28 Aug 2012 03:13:59 AM | Points: 25

Keep it up... My 4! :)

>> Write Response - Respond to this post and get points
Related Posts

Let's start with a series of articles to learn, try to understand and dig more into ASP.NET MVC Framework. As part of it, I'm posting my first article.

As I found many of MVC article but I don't find any article with database interaction with simple functionality. That's the reason I come out with this article.

In today's dot.net world MVC architecture was the new hot cake released in 2009 . In 2012 MVC4 has been released and the developers are moving towards Asp.Net MVC. Let us see why MVC than Web-forms and what are the advantages of MVC over Webforms .

This is the 9th MVC (Model view controller) tutorial and in this article we will try to create outbound navigation links using MVC framework. In case you are completely new to MVC (Model view controller), do have a look at the previous articles and videos given in the table below for quick start.

In the previous ASP.NET MVC lab we saw how we can pass data from controller to the view. So now the next step is to create a simple model and see all the 3 ASP.NET MVC entities (i.e. model, view and controller) in action.

More ...
About Us | Contact Us | The Team | Advertise | Software Development | Write for us | Testimonials | Privacy Policy | Terms of Use | Link Exchange | Members | Go Top
General Notice: If you find plagiarised (copied) contents on this page, please let us know the original source along with your correct email id (to communicate) for further action.
Copyright © DotNetFunda.Com. All Rights Reserved. Copying or mimicking the site design and layout is prohibited. Logos, company names used here if any are only for reference purposes and they may be respective owner's right or trademarks. | 5/20/2013 9:31:50 PM