Global.asax is a file where you can define your application route under Application_Start function. This is global.asax file code from the ASP.NET MVC application which we have created in the previous tutorial.
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Routing;
{
public class MvcApplication : System.Web.HttpApplication
{
protected void Application_Start()
{
AreaRegistration.RegisterAllAreas();
RouteConfig.RegisterRoutes(RouteTable.Routes);
}
}
}
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Routing;
{
public class RouteConfig
{
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute(“{resource}.axd/{*pathInfo}”);
name: “Default”,
url: “{controller}/{action}/{id}”,
defaults: new { controller = “Home”, action = “Index”, id = UrlParameter.Optional }
}
}
}
- name: “Default” => Name of the Route.
- url: “{controller}/{action}/{id}” => It represents that URL will be first Controller, then Action and then Id(Optional)
Note:- When you see URL arrive in the form of {X}/{Y}/{Z}, then the first part is Controller name, second part is Action name and the thirs part is an Id parameter. - defaults: new { controller = “Home”, action = “Index”, id = UrlParameter.Optional } => Our default controller will be Home, default action will be Index, and Id will be optional.
Let’s try to run your application.
When you will run your application, it will show output page and you will see that visual studio has directed the browser to port 50248 (http://localhost:50248/). You will see a different port number because visual studio allocates a random port number when the project is created.
The routing engine maps the URL to the corresponding controller as:
- It looks the controller segment in URL. If this segment is empty then it takes the default controller (E.g. Home).
- Then it looks Action segment in URL. If this segment is empty then it takes the default Action(E.g. Index).
- Id will be ignored because it is optional and not provided.
It means you can request any of the following URLs, and they will be directed to the index action on the home controller.
- http://localhost:50248/
- http://localhost:50248/Home
- http://localhost:50248/Home/Index
Custom ASP.NET MVC Routing
If you want to add your own route, you can do it easily. Just follow the following steps.
Consider we have a page that contains the product of customers. Following is the code which will route to the customer page.
routes.MapRoute(
“Customer”,
“Customer/{action}/{id}”,
defaults: new { controller = “Customer”, action = “Product”, id = UrlParameter.Optional }
);
Step 1 : Add the above code in RegisterRoutes method under RouteConfig.cs class.
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Routing;
{
public class RouteConfig
{
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute(“{resource}.axd/{*pathInfo}”);
“Customer”,
“Customer/{action}/{id}”,
defaults: new { controller = “Customer”, action = “Product”, id = UrlParameter.Optional }
);
name: “Default”,
url: “{controller}/{action}/{id}”,
defaults: new { controller = “Home”, action = “Index”, id = UrlParameter.Optional }
}
}
}
Right click on Controller folder in solution explorer -> Choose Add -> Controller
Now a new CusomterController.cs class file will open.
{
return “This is Customer’s product page”;
}
Borse Hermes
Just a quick note to let you know that I appreciated your insights about the topic. Quite helpful for what I am interested in these days.