ASP.NET MVC Routing

Routing is the process of redirecting an HTTP request to a controller, and the functionality of this processing is implemented in System. Web. Routing. This build is not part of the ASP.NET MVC. It’s actually part of the runtime environment ASP.NET and it was officially released along with ASP.NET how .NET 3.5 SP1.

System. Web. Routing is used by the MVC framework, but it is also used by dynamic data ASP.NET. The MVC framework uses routing to direct the request to the controller. The Global.asax file is the part of your application where you will define the route for your application.

This is the code from the application launch event in Global. asax from the MVC application that we created in the previous chapter.

using System;
using System.Collections.Generic;
using System.Linq;

using System.Web;
using System.Web.Mvc;
using System.Web.Routing;

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

Below is an implementation of the RouteConfig class, which contains a single RegisterRoutes method.

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});
      }
   }
}

You will define routes, and these routes will map URLs to a specific controller action. An action is just a method on the controller. It can also select parameters from this URL and pass them as parameters to the method.

So this route, which is defined in the app, is the default route. As you can see from the code above, when you see that the URL comes in the form (something) / (something) / (something), the first chunk is the controller name, the second chunk is the action name, and the third chunk is the ID parameter.

Understanding routes

MVC applications use a routing system ASP.NET, which decides how URLs are mapped to controllers and actions.

When Visual Studio creates an MVC project, it adds several default routes to get started. When you launch the app, you will see that Visual Studio has directed the browser to port 63664. You will almost certainly see a different port number in the URL that your browser requests, because Visual Studio highlights a random port when creating a project.

In the last example, we added a HomeController, so you can also request any of the following URLs, and they will be directed to the Index action on the HomeController.

HTTP: / / local: 63664 / Home /

HTTP: / / local: 63664 / Home / Index

When the browser requests http: / / mysite / or http: // mysite / Home, it returns output from the Index HomeController method.

You can also try this by changing the URL in your browser. In this example, it is http: / / localhost: 63664 /, except that the port may be different.

If you add / Home or / Home / Index to the URL and click Enter, you will see the same result from the MVC application.

As you can see in this case, the convention is that we have a controller named HomeController, and this HomeController will be the starting point for our MVC application.

The default routes that Visual Studio creates for a new project assume that you will follow this convention. But if you want to follow your own convention, you will need to change the routes.

Customs Convention

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

Let’s look at a simple example. Suppose we have a page that contains a list of processes. Below is the code that will direct you to the process page.

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

When someone logs in and searches for a URL with Process / Action / Id, they access the process controller. We can make the action a little different, the default action, we can make it a List instead of an Index.

Now the incoming request looks like localhosts / process. The routing engine will use this routing configuration to pass it along, so it will use the default action List.

The full implementation of the class is shown below.

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(
            "Process", "Process/{action}/{id}",
 defaults: new{
 controller = " Process", action = "List ", id =
               UrlParameter.Optional});
 
 routes.MapRoute(
 name: "Default", url: "{controller}/{action}/{id}",
 defaults: new{
 controller = "Home", action = "Index", id =
               UrlParameter.Optional});
      }
   }
}

Step 1. Run this and request a process page with the following URL http: / / localhost: 63664 / Process

You will see HTTP 404 because the routing engine is looking for a ProcessController that is not available.

Step 2. Create a ProcessController by right-clicking the Controllers folder in Solution Explorer and selecting Add – > Controller.

The Add Scaffold dialog box appears.

Step 3-Select MVC 5 Controller-Empty option and click”Add”.

The Add Controller dialog box opens.

Step 4-Set a name for ProcessController and click”Add”.

Now you will see a new C # ProcessController.cs file in the Controllers folder, which is also open for editing in Visual Studio.

Now our default action will be List, so we want to have List here instead of Index.

Step 5-Change the return type from ActionResult to string, and also return some string from this action method using the following code.

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 string List(){
         return "This is Process page";
      }
   }
}

Step 6-When you launch this app, you will see the result from the default route again. When you specify the following URL, http: / / localhost: 63664 / Process / List, you will see the result from the ProcessController.

Leave a Reply

Your email address will not be published.

You May Also Like
Read More

Components .NET

A well-designed web application written for ASP.NET, will include separate components that can be organized as separate data…