Routing is the process of directing a HTTP request to a controller and the system. Web. Routing implements the functionality of this processing. This setting does not form part of ASP.NET MVC. It is part of the ASP.NET runtime and has been officially released as a. NET 3.5 SP1 with the ASP.NET.

System. Web. Routing is used in the MVC framework but ASP.NET Dynamic Data is also used. The MVC framework uses the routing to direct an application to a controller. The Global.asax file is part of your application in which you define your application route.

This is the code of the Global.asax app start event from the MVC app that we created in the previous chapter.

namespace MVCFirstApp
{
    public class MvcApplication : System.Web.HttpApplication
    {
        protected void Application_Start()
        {
            AreaRegistration.RegisterAllAreas();
            RouteConfig.RegisterRoutes(RouteTable.Routes);
        }
    }
}

This is the implementation of the RouteConfig class, which includes a RegisterRoutes method.

namespace MVCFirstApp
{
    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 }
            );
        }
    }
}

You will define the routes and map the URLs to a particular controller action. An action is a controller method. You can also select parameters from the URL and pass them to the method as parameters. This route specified in the application is therefore the default route. As shown in the above code.

when you see a URL coming in the form of (something)/(something)/(something), the first piece is the name of the controller, the second piece is the name of the action, and the third piece is an ID.

Understanding ASP.NET MVC Routes

The MVC applications use the ASP.NET routing system that determines how controllers and actions map URLs.

When Visual Studio creates the MVC project, a number of default routes are added to start us. You will see that Visual Studio sent the browser to port 63664 when you run your application. You will almost certainly see another port number in your browser’s URL because Visual Studio allocates a random port when the project is created.

ASP.NET MVC Routing

In the last example, we have added a HomeController so that you can also request any of the following URLs and direct them to the HomeController index action.

http://localhost:5670/Home/

http://localhost:5670/Home/Index

When a browser requests http:/mysite/ or http:/mysite / Home, the output is returned from the index method of HomeController. You can also try this by changing the URL of your browser.

It’s http:/localhost:5670/ in this example, except that the port may be different. If you add /Home or /Home/Index to the URL and press the ‘Enter’ button, the MVC application will show the same result.

ASP.NET MVC Routing

As you can see in this case, the convention is that we have a HomeController, which is the starting point for our MVC application.

Visual Studio’s default routes for a new project assume that you are going to follow this convention. But if you want to adhere to your own convention, you need to change the routes.

ASP.NET MVC Custom Convention

You can add your own routes. If you don’t like these action names, if you have different ID parameters or if you usually have a different URL for your site, you can add your own route entries.

Let’s take a quick look at an example. Bear in mind that we have a page that contains the process list. The code below will lead to the process page.

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

If someone enters and searches for a Process / Action / Id URL, they go to the Process Controller. We can make the action a bit different, we can make the default action a list rather than an index.

A request that comes now looks like hosts / process. The routing engine uses this routing configuration to pass it on, so that it uses the default List action.

The complete class implementation is as follows.

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)
        {
            routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

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

            routes.MapRoute(
            "Process", "Process/{action}/{id}",
            defaults: new { controller = " Process", action = "List ", id = UrlParameter.Optional }
            );
        }
    }
}

Step 1 − Run this and ask for a process page with http:/localhost:5670/process URL

ASP.NET MVC RoutingYou ‘ll see an HTTP 404 because the routing engine is looking for a not available ProcessController.

Step 2 − Right – click on the Controllers folder in the Solution Explorer to create ProcessController and select Add Controller.

ASP.NET MVC Routing

The Add Scaffold dialog will be displayed.

ASP.NET MVC Routing

Step 3 − Choose the MVC 5 Controller–Empty option and then click ‘ Add. ‘ The Add Controller dialog is shown.

ASP.NET MVC Routing

Step 4 − Set the ProcessController name and press the ” Add ” button.

You will now see a new C # file ProcessController.cs in the Controllers folder that is also available for editing in Visual Studio.

ASP.NET MVC RoutingNow our default action is a list, so we want a list action instead of an index here.

Step 5 − Change the return type from ActionResult to string and return a string using the following code from this action method.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace MVCFirstApp.Controllers
{
    public class ProcessController : Controller
    {
        // GET: Process
        public ActionResult Index()
        {
            return View();
        }

        public string List()
        {
            return "This is Process page";
        }
    }
}

Step 6 − After running this application, you will see the result of the default route again. If you specify the following URL, http:/localhost:5670/Process/List, the results of the ProcessController will be shown.

ASP.NET MVC Routing

Subscribe to our newsletter

You can unsubscribe anytime you want