Custom Routing in ASP.NET Core MVC

Custom Routing in ASP.NET Core MVC Application

In this article, I am going to discuss Custom Routing in the ASP.NET Core MVC Web Application. Please read our previous article, where we discussed the basics Concepts of Routing and the fundamentals of Conventional Based Routing in ASP.NET Core MVC Applications. We will work with the same example we created in our previous article. As part of this article, we are going to discuss the following pointers in detail.

  1. Custom Routing in ASP.NET Core MVC Application
  2. Routing without Default Values in ASP.NET Core MVC Application
  3. Route Constraints in ASP.NET Core MVC Web Application
  4. How to Make Route Parameters Optional in ASP.NET Core MVC Application?
  5. How to Provide Default Route Values in ASP.NET Core MVC Application?
Custom Routing in ASP.NET Core MVC Application

Custom routing in ASP.NET Core MVC allows us to define our own URL routing patterns for our web application, giving us more control over how URLs are mapped to controller actions. This can be useful when creating SEO-friendly URLs, handling specific routing scenarios, or creating a more organized and structured URL hierarchy.

Custom Routing without Default Values in ASP.NET Core MVC Application:

ASP.NET Core MVC allows configuring routing without default values for route parameters (controller, action, and route data). This allows us to create routes that depend only on the URL values without any predetermined or default values. If a route parameter is absent from the URL, it will be treated as missing.

Suppose you want to define your own custom route without default values. In that case, you need to modify the MapControllerRoute middleware component within the Main method of the Program class, as shown in the below image. As you can see, we have removed the default values as well as the Optional Parameter from the Pattern (pattern: “{controller}/{action}”)). You can give any name to your route, and here I am providing the Route name as CustomRoute (name: “CustomRoute”).

Custom Routing in ASP.NET Core 6 MVC Web Application

With the above Custom Routing in Place, when we run the application, it will not access the Home Controller Index Action method by default. That means it will not access any controller action method by default. The above MapControllerRoute is the simplest possible convention-based route for an ASP.NET Core MVC Web Application. Now run the application and navigates to the following URLs, and you will see the output as expected. You need to change the port number.
https://localhost:44359/Student/Details
https://localhost:44359/Student/Index

This is working fine. However, what if we wanted to have more specific routes? Say, something like the following URLs:
https://localhost:44359/Student/Details/20
https://localhost:44359/Student/Index/10

If you want your controller action methods to match the above URLs, you need to use a Route Constraints feature in ASP.NET Core MVC Application.

Route Constraints in ASP.NET Core MVC Web Application:

Route constraints in ASP.NET Core MVC are rules you can apply to route parameters to specify certain criteria that the parameter must meet for the route to match a given URL. Constraints allow you to restrict the values that are accepted by route parameters, ensuring that the route is only matched for valid inputs. They allow us to add validation or filtering logic to our route parameters, ensuring that only specific values are considered valid and eligible for routing.

Route constraints are typically used in both attribute-based and conventional-based routing to ensure that the values provided in the URL match the expected data types or formats. They help improve the accuracy of routing and provide a mechanism for handling validation of route parameters.

Route constraints are particularly useful when we want to restrict the values that can be passed as route parameters, ensuring that they conform to a specific format or meet certain criteria. Let’s say we want to create a route that will match the following URLs.
https://localhost:44359/Student/Details/20
https://localhost:44359/Student/Index/10

We can achieve this in two ways, i.e., Attribute Routing and Conventional Based Routing. We will discuss How to achieve the same using Attribute Routing in our upcoming articles. In this article, let us see how we can achieve the same using Conventional Based Routing in ASP.NET Core MVC Web Application.

In order to achieve the above URL Patterns, we need to modify the MapControllerRoute Middleware Component as follows. As you can see, as part of the pattern, we specify the id parameter (pattern: “{controller}/{action}/{id}”). Here, the id parameter is not optional, it is mandatory, and while accessing any action method, it is mandatory to pass the Id parameter value.

Route Constraints in ASP.NET Core MVC Web Application

As we make the action method to take the Id parameter value mandatory, so we need to change the action methods of our controller with the Id parameter. So, modify the StudentController class as shown below.

using Microsoft.AspNetCore.Mvc;
namespace RoutingInASPDotNetCoreMVC.Controllers
{
    public class StudentController : Controller
    {
        public string Index(string id)
        {
            return $"Index({id}) Action Method of StudentController";
        }
        public string Details(string id)
        {
            return $"Details({id}) Action Method of StudentController";
        }
    }
}

With the above changes in place, now run the application and navigate to the following URLs, and you will see that methods are executed as expected.
https://localhost:44359/Student/Details/20
https://localhost:44359/Student/Index/10

This is working fine. But, the problem with the above route is that it can accept any value. Instead of an integer, if you pass a string value, it also accepts and executes the action methods, as shown below.
https://localhost:44359/Student/Details/ABC
https://localhost:44359/Student/Index/ABC

If you want to restrict the id parameter value to be an integer only, then you need to use a concept called Route Constraint in ASP.NET Core. So, what we need to do is, while defining the Route, explicitly, we need to tell that this parameter is going to accept only integer or boolean or any particular data type value.

In our example, we want to restrict the id parameter to accept only integer values. So, we need to modify the MapControllerRoute Middleware Component as follows. As you can see, as part of the pattern, we specify the id parameter to accept int values only (pattern: “{controller}/{action}/{id:int}”).

Custom Route Constraints in ASP.NET Core MVC Web Application

Note: The {id:int} in the pattern of MapControllerRoute Middleware Component specifies that whatever is in this part of the URL must be an integer. Otherwise, the URL does not map to this route.

With the above changes in place, now run the application and navigate to the following URLs, and you will see a 404 error. This is because we are passing the Id parameter value as ABC here.
https://localhost:44359/Student/Details/ABC
https://localhost:44359/Student/Index/ABC

Now pass the id parameter value as an integer, and you should get the output as expected. There are many route constraints available that you can use. Some of them are as follows.

  • :int or :int(min,max): Restricts the parameter to integer values within the specified range.
  • :long, :double, :float, :decimal: Constraints for other numeric types.
  • :guid: Restricts the parameter to valid GUID (Globally Unique Identifier) format.
  • :bool: Restricts the parameter to true or false.
  • :alpha, :alpha-numeric, :regex(pattern): Constraints based on regular expressions.
  • :length(min,max): Restricts the length of the parameter value.

Route constraints are a powerful way to control the flow of your application and ensure that the incoming data matches your expectations, enhancing the reliability and security of your routes.

How to Make Route Parameters Optional in ASP.NET Core MVC Application?

In ASP.NET Core MVC, you can make route parameters optional by specifying default values for those parameters. This approach allows you to define routes that can match URLs with or without specific route parameter values. Before understanding How to Make the Route Parameter an Optional Parameter, let us first change the StudentController class as shown below. 

using Microsoft.AspNetCore.Mvc;
namespace RoutingInASPDotNetCoreMVC.Controllers
{
    public class StudentController : Controller
    {
        public string Index()
        {
            return $"Index() Action Method of StudentController";
        }
        public string Details(string id)
        {
            return $"Details({id}) Action Method of StudentController";
        }
    }
}

As you can see in the above code, the Index action method does not take any parameter, while the Details action method takes one parameter. Now we need to invoke the Index action method without a parameter as follows.
https://localhost:44359/Student/Index

On the other hand, we need to make the id parameter of the Details action method optional. It means the Details action method should be invoked using the following two URLs.
https://localhost:44359/Student/Details
https://localhost:44359/Student/Details/10

In order to achieve this, we need to use optional parameters in our convention-based routing by adding a question mark “?” to the optional route parameter’s constraint. In our example, we want to mark the id parameter as an optional parameter as well as we also want this parameter to accept only integer values. So, in the URL pattern, we need to specify the id parameter as “id:int?“. So, we need to modify the MapControllerRoute Middleware Component as follows.

How to Make Route Parameters Optional in ASP.NET Core MVC Application

Note: Here, “id:int?” tells that id is an optional parameter, but if you pass any value, then it should be of type integer. You can define only one optional parameter per route, and that optional parameter must be the last parameter.

With the above changes in place, now run the application and navigate to the following URLs, and you will get the data as expected.
https://localhost:44359/Student/Index
https://localhost:44359/Student/Details
https://localhost:44359/Student/Details/10

How to Provide Default Route Values in ASP.NET Core MVC Application?

In ASP.NET Core MVC, the default route allows us to specify default values for route parameters in our application’s routing configuration. Route parameters are placeholders in the URL pattern that capture values from the URL and pass them to your controller actions.

Default route values are particularly useful when you have optional route parameters or are commonly used with a specific value. Instead of specifying the same value for a route parameter in every URL or in every action method that generates URLs, you can set a default value for that parameter. This simplifies your code and makes your URLs more readable and user-friendly.

As of now, you can observe that whenever we run the application, by default, it loads the base URL (https://localhost:44359/) of the application and give us a 404 error. This is because we have not set any default values for our Route parameter. If we have not specified the Controller or Action method name in the URL, what should the Controller and Action method execute?

Let us proceed and understand how we can specify the default values for our Route Parameter so that if we do not specify the Controller or Action method name in the URL or when the application launch, it should take the default values from the Route and execute the action method.

So, using Default values, we can specify what happens if parts of the route are not provided in the URL. For example, when we navigate to the following two URLs
https://localhost:44359/
https://localhost:44359/Home

We want to map the above two URLs to the Home Controller and Index action method of the Application. To do so, we need to specify the default controller and action method name in the MapControllerRoute Middleware Component URL Pattern. So, modify the MapControllerRoute Middleware Component within the Main method of the Program class as follows. Here, we have specified the default controller name as Home, the default action method name as Index, and Id as the Route parameter, which is optional as well as that parameter can accept only integer values (pattern: “{controller=Home}/{action=Index}/{id:int?}“).

How to Provide Default Route Values in ASP.NET Core MVC Application

With the above changes in place, now run the application and visit the following two URLs, and you should get the output as expected.
https://localhost:44359/
https://localhost:44359/Home

You can also map the default values for the route parameter by using the defaults parameter of the MapControllerRoute Extension method, as shown in the below image. 

Custom Routing in ASP.NET Core MVC Application

The following is the complete Program.cs class file code.

namespace RoutingInASPDotNetCoreMVC
{
    public class Program
    {
        public static void Main(string[] args)
        {
            var builder = WebApplication.CreateBuilder(args);

            // Add services to the container.
            builder.Services.AddControllersWithViews();

            var app = builder.Build();

            // Configure the HTTP request pipeline.
            if (!app.Environment.IsDevelopment())
            {
                app.UseExceptionHandler("/Home/Error");
                // The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
                app.UseHsts();
            }

            app.UseHttpsRedirection();
            app.UseStaticFiles();

            app.UseRouting();

            app.UseAuthorization();

            app.MapControllerRoute(
                name: "CustomRoute",
                pattern: "{controller}/{action}/{id:int?}",
                defaults: new { controller = "Home", action = "Index" }
            );

            app.Run();
        }
    }
}

Default route values provide a convenient way to reduce URL generation and routing code duplication. They also contribute to creating consistent and user-friendly URLs throughout your application.

In the next article, I am going to discuss Attribute Routing in ASP.NET Core MVC Applications. Here, in this article, I try to explain Custom Routing in ASP.NET Core MVC Web Application with Examples. I hope you enjoy this Custom Routing in ASP.NET Core MVC Web Application article.

Leave a Reply

Your email address will not be published. Required fields are marked *