csharp

Routing In MVC

In this article, we learn about Routing In MVC.

Routing In MVC
Routing is an important part of MVC it defines the URL pattern and handler information. All the configured routes of an application stored in RouteTable and will be used by Routing engine to determine appropriate handler class or file for an incoming request.

Routing: How the Web Site Knows Which Controllers and Methods to Request
ASP.NET MVC uses ASP.NET routing to control how the application converts URLs. It also manages which
controllers and methods the incoming HTTP requests target and hence which views to display. To explain
how this works we’ll examine the default route that was set up when we created the project.
Open the file RouteConfig.cs file, which is located in the App_Start folder. The code contained in this
file defines rules that the application uses to target the correct controller and method depending on the URL
entered into the web browser. The code listing is as follows:

Image
[php]
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Routing;

namespace MVCDEMO
{
public class RouteConfig
{
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
}
}
}

[/php]
The code that routes.MapRoute method. This defines a route named “Default”. The code URL: “{controller}/{action}/{id}” read the site to expect the controller name first, followed by the action (which matches up with the method name like “Index” in the controller).

The third entry named defaults tells the application what to target when there is no controller or
action specified in the URL. This default tells the application to target the Index method of the Home
Controller, which in turn calls the Views\Home\Index.cshtml page. This is why this page loads when the site
is run with any additional parameters in the URL.

If the URL doesn’t contain anything after domain name then the default controller and action method will handle the request. For example, http://lcoalhost:1235 would be handled by HomeController and Index method as configured in the defaults parameter.

Multiple Routes
You can also configure a custom route using the MapRoute extension method. You need to provide at least two parameters in MapRoute, route name, and URL pattern. The Defaults parameter is not mandatory.

You can register multiple custom routes with different names. Consider the following example where we register “Employee” route.

As shown in the below code, URL pattern for the Employee route is employees/{id}, which specifies that any URL that starts with the domain name “students”, must be handled by given controller like “Employee”. Notice that we have not given {action} in the URL pattern because we want every URL that starts with Employee should always use Index action of Employee Controller. We have specified default controller and action to handle any URL request which starts from domain name employes.
[php]
public class RouteConfig
{
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

routes.MapRoute(
name: "Employee",
url: "employees/{id}",
defaults: new { controller = "Employee", action = "Index"}
);

routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
}
}
[/php]

Key Points:
Why Use Routing in MVC?

  • Routing is an important part of MVC. It maps URL to physical path.
  • We can configured multiple custom route.
  • It Manage URL Pattern.
  • It Contains Handler Information. URL pattern starts after domain name.
  • Routes can be configured in RouteConfig class
  • It is mandatory to Route must be registered in Application_Start event in Global.ascx.cs file.
  • Leave a Reply

    Your email address will not be published.