The ASP.NET Routing module is responsible for mapping incoming browser requests to particular MVC controller actions.
When you create a new ASP.NET MVC application, the application is already configured to use ASP.NET Routing in Web.config
file.
<system.webServer>
<modules>
<remove name="TelemetryCorrelationHttpModule" />
<add name="TelemetryCorrelationHttpModule"
type="Microsoft.AspNet.TelemetryCorrelation.TelemetryCorrelationHttpModule, Microsoft.AspNet.TelemetryCorrelation"
preCondition="integratedMode,managedHandler" />
</modules>
</system.webServer>
Be careful not to delete these sections because without these sections routing will no longer work.
The routes are registered in the Global.asax
file by invoking a RegisterRoutes
method in the Application_Start()
method. A route table is created in the App_Start\RouteConfig.cs
file.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Routing;
namespace MvcWithEF6Demo
{
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 }
);
}
}
}
MapRoute()
method is an extension method and we have to define the route name
which is default;controller
, action
and id
.Home
controller, Index
action and id
which is optional.In many simple ASP.NET MVC applications, the default route table will work, but you can also create a custom route. You can register multiple custom routes with different names.
using System.Web.Mvc;
using System.Web.Routing;
namespace MvcWithEF6Demo
{
public class RouteConfig
{
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
name: "Author_Details",
url: "authors/{id}",
defaults: new { controller = "Author", action = "Details" }
);
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
}
}
}
In the above code, the URL pattern for the Author_Details route is authors/, which specifies that any URL that starts with *domain/authors, must be handled by AuthorController
.
Details
action of AuthorController
.The following table shows which Controller
, Action
method and Id
parameter would handle different URLs considering above default route.
URL | Controller | Action | Id |
---|---|---|---|
http://localhost:58379/ | HomeController | Index | |
http://localhost:58379/Author/ | AuthorController | Index | |
http://localhost:58379/Author/Create | AuthorController | Create | |
http://localhost:58379/Author/Edit/2 | AuthorController | Edit | 2 |
http://localhost:58379/Author/Delete/3 | AuthorController | Delete | 3 |
http://localhost:58379/Authors/1 | AuthorController | Details | 1 |
The controller and action values in the route are not case-sensitive and the URL route patterns are relative to the application root.