;

How to Create a Custom Routes in ASP.NET MVC C#


Tutorialsrack 18/02/2021 C# ASP.NET MVC

In this article, we will learn how to create custom routes in ASP.NET MVC C#. Routing is one of the primary aspects of the MVC framework which makes MVC what it is. For most of the ASP.NET web applications, the default route will work fine. However, you would possibly discover that you simply have specialized routing needs. In that case, you'll create a custom route.

For example, you are creating a blog using ASP.NET MVC, where you need a URL pattern like this:

 “/blog/sql

For achieving this URL pattern, you need custom routing.

So in this article, you will learn how to implement custom routing in ASP.NET MVC?

By the end of the article, you will learn how to create custom routing in an ASP.NET MVC web application.

As we discuss in the previous article, Routing is a pattern matching mechanism that is responsible for mapping the incoming request from the browser to specific controller actions. So I am not discussing routing in this article. You can learn more about Routing in ASP.NET MVC

Let's discuss how to create your own custom route in ASP.NET MVC web Application

Custom Routing in ASP.NET MVC

You can configure a custom route by using the MapRoute() Extension method of RouteCollection Class. You need to define at least two parameters of the MapRoute method. i.e. The first parameter is the route name and the second parameter is the URL pattern for the Route. And the third parameter is the default, which is optional. 

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

Example 1: Custom Route for Blog controller and ArticleByCategory Action methods

Example 1: Custom Route for Blog controller and ArticleByCategory Action methods
public class RouteConfig {
  public static void RegisterRoutes(RouteCollection routes) {
    routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

    //Custom Route
    routes.MapRoute(
    name: "Blog", //RouteName
    url: "blog/{category}", //Route URL Pattern

    // Controller and Action Method for Above Defined URL Pattern
    defaults: new {
      Controller = "Blog",
      action = "ArticleByCategory"
    });

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

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

Code - Add Blog Controller to our web Application
public class BlogController: Controller 
{
  // GET: Blog
  public ActionResult Index() {
    return View();
  }
  
  public ActionResult ArticleByCategory(string category) {
    return View();
  }
}

Code Explanation

As you see in the above example, the URL pattern for the Blog route is "blog/{category}" which specifies that any URL which starts from https://www.example.com/blog/category after the domain name must be handled by the action method “ArticleByCategory” of the Blog controller. You have noticed that in this example we specified the specific action method of the “Blog” controller so that every URL that starts with the “blog/{category}” must hit the specific action method of the controller. In the above custom route for the “Blog” controller, you have noticed that we are not defining any optional parameter for that route, so if you want to hit the action method “ArticleByCategory” then you must pass the category with the URL, otherwise, it will not hit the action method “ArticleByCategory” of the “Blog” controller. 

In ASP.NET MVC, the MVC framework evaluates each route in sequence. It starts with the first configured route and if the incoming requested URL doesn’t match the first URL pattern of the route then it will evaluate the second route and so on. 

The following URLs will be mapped to the Blog route.

  • https://www.example.com/blog/sql
  • https://www.example.com/blog/charp
  • https://www.example.com/blog/html
  • https://www.example.com/blog/python

I hope this article will help you to understand how to create a custom route in ASP.NET MVC.

Share your valuable feedback, please post your comment at the bottom of this article. Thank you!


Related Posts



Comments

Recent Posts
Tags