Loading

ASP.NET MVC

What is Creating Custom Routes in ASP.NET MVC?. The Complete ASP.NET MVC Developer Course 2023 [Videos].

In this article, I am going to discuss How to Create Custom Routes in the ASP.NET MVC Applications with Examples. Please read our previous article before proceeding to this article where we discussed the basics of Routing in the ASP.NET MVC Application. At the end of this article, you will understand the following pointers in detail which are related to ASP.NET MVC Custom Routing.


  1. What is Custom Routing in MVC?
  2. Why do we need Custom Routing in ASP.NET MVC?
  3. How to Create Custom Route in ASP.NET MVC?

As we already discussed the ASP.NET MVC Routing is a pattern matching mechanism that handles the incoming HTTP request (i.e. incoming URL) and figures out what to do with that incoming HTTP request. The following diagram shows how the routing work in ASP.NET MVC Framework and in our previous Video, we already discussed this.

Custom Routing in ASP.NET MVC Application

In our previous Video, we discussed the default route that is created by ASP.NET MVC Framework and how it handles the incoming request. But if you dont want to follow the default URL Pattern rather you want to create your own URL Pattern then you need to either modify the default route or you need to create your own route. Lets discuss how to create our own route in ASP.NET MVC application.

Creating Custom Routes in ASP.NET MVC Application:

As we already discussed, if we want to configure any routes then we need to configure the routes within the RegisterRoute method of RouteConfig class using the MapRoute extension method. While configuring the Routes, at least two parameters we need to provide to the MapRoute method i.e. Route name and URL pattern. The Default parameter is optional. 

The point that you need to remember is, the Route Names must be unique. You can register multiple custom routes with different names. Consider the following example where we register the “Employee” route.

namespace FirstMVCDemo
{
public class RouteConfig
{
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
name: "Employee",
url: " Employee/{id}",
defaults: new { controller = "Employee", action = "Index" }
);
routes.MapRoute(
name: "Default", //Route Name
url: "{controller}/{action}/{id}", //Route Pattern
defaults: new
{
controller = "Home", //Controller Name
action = "Index", //Action method Name
id = UrlParameter.Optional //Defaut value for above defined parameter
}
);
}
}
}

So, in this way you can configure as many as routes you want with your own URL pattern in ASP.NET MVC Application. Lets add Employee Controller to our application

namespace FirstMVCDemo.Controllers
{
public class EmployeeController : Controller
{
public ActionResult Index()
{
return View();
}
}
}
Code Explanation:

The URL pattern for the Employee route is Employee/{id} which specifies that any URL that starts with domainName/Employee, must be handled by EmployeeController. Notice that we havent specified {action} in the URL pattern because we want every URL that starts with Employee should always use Index action of EmployeeController. We have specified the default controller and action to handle any URL request which starts from the domain name/Employee.

The ASP.NET MVC framework evaluates each route in sequence. It starts with the first configured route and if the incoming URL doesnt satisfy the First URL pattern of the route then it will evaluate the second route and so on. In the above example, the routing engine will evaluate the Employee route first and if the incoming URL doesnt start with domainName/Employee then only it will consider the second route which is the default route.

The following URLs will be mapped to the Employee route.

  1. http://localhost:53605/Employee
  2. http://localhost:53605/Employee/Index
  3. http://localhost:53605/Employee/Index/3

Note: Always put the more specific route on the top order while defining multiple routes, since the routing system checks the incoming URL pattern from the top and as it gets the matched route it will consider that. It will not check further routes after the matching pattern.

See All

Comments (273 Comments)

Submit Your Comment

See All Posts

Related Posts

ASP.NET MVC / Youtube

What is MVC?

MVC is an architectural software design pattern that is used for developing interactive applications where their user interaction is involved and based on the user interaction some event handling has occurred. It is not only used for web-based applications but it can also be used for Desktop or mobile-based applications where there are user interactions involved.
28-jan-2022 /28 /273

ASP.NET MVC / Youtube

How to Creat First ASP.NET MVC Application using Visual Studio?

In this article, I am going to discuss how to create the first ASP.NET MVC Application step by step from scratch using Visual Studio 2015. You can use any version as per your choice but the step will remain the same. Please read our previous article before proceeding to this article where we gave a brief introduction to ASP.NET MVC Framework.
28-jan-2022 /28 /273

ASP.NET MVC / Youtube

What is ASP.NET MVC File and Folder Structure?

In this article, I am going to discuss the auto-generated ASP.NET MVC File and File Structure when we create a new ASP.NET MVC application. Please read our previous article before proceeding to this article where we discussed how to create ASP.NET MVC 5 application step by step from scratch.
28-jan-2022 /28 /273