| by Achyut Kendre | 5 comments

What is Url Routing? How it works in ASP.NET MVC 5?


Example Link : https://drive.google.com/file/d/1LdPfsfpnRy0BytOUCaLJnauN6_9hRpnR/view

The ASP.NET Routing module/Route Engine 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. ASP.NET MVC uses Route Table for routing and Route table contains entires/recors called route and every route is composed of url pattern and handling mechnism or code.

How Routing Works?

In simple word, we can say that ASP.NET MVC Routing is a pattern matching mechanism that handles the incoming request (i.e. incoming URL) and figures out what to do with that incoming request (i.e. incoming URL). At runtime, Routing engine uses the Route table for matching the incoming request’s URL pattern against the URL patterns defined in the Route table.

ASP .NET Routing

ASP.NET Routing is setup a route table in the application’s Global.asax file. The Global.asax file is a special file that contains event handlers for ASP.NET application lifecycle events. The route table is created during the Application Start event.

namespace MVCFirstApp {
   public class MvcApplication : System.Web.HttpApplication {
      protected void Application_Start(){

Following is the implementation of RouteConfig class, which contains one method RegisterRoutes.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Routing;
namespace MVCFirstApp {
   public class RouteConfig {
      public static void RegisterRoutes(RouteCollection routes){
            name: "Default",
            url: "{controller}/{action}/{id}",
            defaults: new{ controller = "Home", action = "Index", id = UrlParameter.Optional});

As you can see in the above code, the Routing is configured using the MapRoute() extension method of RouteCollection class, where the Route name is “Default” and the URL pattern is “{controller}/{action}/{id}“. The Defaults value for the controller is Home, and the default action method is Index and the id parameter is optional.

Example to understand Routing:
http://localhost:53605/ => controller = Home, action = Index, id = none, since default value of controller and action are Home and Index respectively.
http://localhost:53605/Home => controller = Home, action = Index, id = none, since default value of action is Index
http://localhost:53605/Home/Index => controller = Home, action = Index, id=none
http://localhost:53605/Home/Index/5 => controller = Home, action = Index, id = 5

Creating Custom Routes in ASP.NET MVC Application:

You can configure your custom route using the MapRoute method of route collection. You need to provide at least two parameters in MapRoute function i.e. route name and URL pattern. The Default parameter is optional.

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)
                name: "Employee",
                url: " Employee/{id}",
                defaults: new { controller = "Employee", action = "Index" }
                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. Let’s add Employee Controller to our application

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

Note: More specific routes should be registered before the genera route, otherwise route engine will map all request to general route and specific route will not be executed.



October 26, 2023 at 5:25 am Reply

I visited various websites however the audio quality for audio songs current at this web page
is actually excellent.


October 30, 2023 at 2:04 am Reply

I am sure this paragraph has touched all the internet visitors,
its really really nice paragraph on building up new webpage.


November 3, 2023 at 12:10 pm Reply

Hello just wanted to give you a quick heads up. The text in your article seem to be running off the screen in Firefox.
I’m not sure if this is a format issue or something to do with web browser compatibility
but I figured I’d post to let you know. The layout look great though!
Hope you get the issue resolved soon. Many thanks

Video Marketing Service in Fort Lauderdale

November 17, 2023 at 2:38 pm Reply

Ahaa, its good dialogue regarding this article here at this webpage, I have read all that, so at this
time me also commenting here.

Best Winter Park Web Development Company

November 18, 2023 at 7:26 am Reply

Hi! Someone in myy Facebook group shared this website wuth us so I
came tto look it over. I’m definitely loving the information. I’m book-marking and
will be tweeting this to my followers! Terrific blogg and superb style
and design.

Leave a Reply