articles

Home / DeveloperSection / Articles / Routing in ASP.NET MVC

Routing in ASP.NET MVC

Chris Anderson 12330 01-Oct-2011

A route is a URL pattern that is mapped to a handler. The handler can be a physical file, such as an .aspx file in a Web Forms application. Routing module is responsible for mapping incoming browser requests to particular MVC controller actions.

MVC3 routes are defined in the Global.asax.cs file of your MVC3 web application. 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 but must be unique, the URL template, and the parameter defaults. The default route that is pre-defined for you maps to Controller/Action/id. In the above, the MapRoute function will create a System.Web.Routing.Route instance and add it to routes collection. A route will determine whether a URL is matched with its definition and which controller will process the URL.

HomeController.cs
using System.Web.Mvc;

 
namespace MVCApplication.Controllers
{
     public class HomeController : Controller //HomeController is the name of controller
    {
         public ActionResult Index(string id) //Index is the name of Action and id is the 
 
         {
            return View();
         }
    }


You can add additional routes by copying the same line and adjusting the URL parameters and the related default values.

     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 Parameter defaults
            );
            routes.MapRoute(
                "MyRoute", // Route name
                "{controller}/{action}/{Category}", // URL with parameters
                new { controller = "Home", action = "Product", Category = (string)null } 
              // Parameter define controller,action and urlparameter
            );
         }    

The above custom route map defines Home/Product/Category. Home denotes a Controller, Product denotes an Action method in controller and Category is parameter through which we can send category value along with URL.

When an MVC application first starts, the Application_Start() method is called. This method, in turn, calls the RegisterRoutes() method. The RegisterRoutes() method creates the route table. Initially, single route created in a route table and if we create as many as routes it will be added in the route table.

protected void Application_Start()

{
      AreaRegistration.RegisterAllAreas();
     RegisterRoutes(RouteTable.Routes);
}

 How to send the route parameter through URL?

1) Create Index view and write sample code as given below and also create
Product view  too.
<html>

<head runat="server">
     <title>Index</title>
</head>
<body>
     <div>
         <h2>
            <a href="Home/Product/2">Product</a>
         </h2>
     </div>
</body>
</html>


2) Create a Controller as below:
using System.Web.Mvc;

namespace MVCApplication.Controllers
{
     public class HomeController : Controller
    {
         public ActionResult Index(string id)
         {
            return View();
         }
         public ActionResult Product(int id)
         {
              Response.Write(“Category Value: “+id);
            return View();
         }
    }
}


Routing in ASP.NET MVC

Routing in ASP.NET MVC

The above example shows how can we pass route parameter through URL utilize its value in another View.



Updated 07-Sep-2019
hi I am software developer at mindstick software pvt. ltd.

Leave Comment

Comments

Liked By