ASP.Net MVC3 Routing


Content:

What is routing? In mvc

Routing in an ASP.NET module is responsible for mapping incoming browser requests to a particular MVC controller action. Incomming Browser request means the URL request. Like

www.shirnandi.com/home/id=5

When Requests arrive to an ASP.NET MVC-based web application it actually first passes through the UrlRoutingModule object, which is an HTTP module.

This module parses the request; i.e. the request from the URL and performs route selection. The UrlRoutingModule object selects the first route object that matches the current request.

If no routes match, the UrlRoutingModule object does nothing and lets the request fall back to the regular ASP.NET or IIS request processing.

Now From the selected Route object, the UrlRoutingModule object contains the IRouteHandler object that is associated with the Route object. This is an instance of MvcRouteHandler. The IRouteHandler instance creates an IHttpHandler object that passes it the IHttpContext object. By default, the IHttpHandler instance for MVC is the MvcHandler object. The MvcHandler object then selects the controller that will ultimately handle the request.

See when an ASP.NET MVC web application runs in IIS 7.0, no file name extension is required for MVC projects. So you cannot find the .aspx,.ascx,.asmx extension.

Now when you create a new ASP.NET MVC application, it will automatically configure to use ASP.NET Routing. ASP.NET Routing is set up in two places:

  1. Webconfig File
  2. Global.asax file

Now interestingly when you create a MVC2 application a route table is automatically created in the application's Global.asax file.
As we all know the Global.asax contains event handlers for ASP.NET application lifecycle events. The route table is created during the Application Start event. So when you open the Global.asax file you will see the following code:

using System;

using System.Collections.Generic;

using System.Linq;

using System.Web;

using System.Web.Mvc;

using System.Web.Routing;

 

namespace MvcApplication1

{

    // Note: For instructions on enabling IIS6 or IIS7 classic mode,

    // visit http://go.microsoft.com/?LinkId=9394801

 

    public class MvcApplication : System.Web.HttpApplication

    {

        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);

        }

    }

}

 
See here the routing is occuring in the "RegisterRoutes" method.

See in the routes.MapRoute method we are passing 3 parameters. 


  1. Controller(Name of the controller)
  2. Action (The method inside the controller)
  3. id (Parameter) 


See in the above url before home it is domain. The home is control. The action method is the index with parameter id.
Now in the home controller the code will be:

using System.Web.Mvc;
namespace MvcApplication1.Controllers
{
    
    public class HomeController : Controller
    {
        public ActionResult Index(int id)
        {
            return View();
        }
    }
}
Conclusion: So in this article we have seen what the functionality of routing in ASP.Net MVC 3 is. 

erver'>
Up Next
    Ebook Download
    View all
    Learn
    View all