Routing In ASP.NET And ASP.NET MVC With Example

What is Routing?

URL Routing or URL Rewriting is a technique using that we can define user-friendly URL which is useful for Search Engine Optimization (SEO).

Using ASP.NET routing or ASP.NET MVC Routing we can achieve that easily.

Rather than calling a physical File name like 'Home/about.aspx' we can use 'Home/about', User cannot remember files extension like .aspx or .php so we can use Routing and make URL easy to remember for them.

Routing in ASP.NET

Let's create ASP.NET Routing first.

Step 1: Create a new Web Application Project and add 2 pages like Default.aspx and About.aspx.

 ASP.NET Empty Web Site

 Add web form

 Solutions Explorer

Step 2: Add Global Application class 'Global.asax'.

 Add Global Application class 'Global.asax'.

Step 3: Add reference System.Web.Routing in your project.

Add reference System.Web.Routing

Step 4: Open global.asax file and add the following code in 'Application_Start'

void Application_Start(object sender, EventArgs e)   
{  
    // Code that runs on application startup
    System.Web.Routing.RouteTable.Routes.MapPageRoute("Home","Home/Index","~/Default.aspx");
    System.Web.Routing.RouteTable.Routes.MapPageRoute("About","Home/About-WDI","~/About.aspx");
}

 

 RouteTable

Here we are going to use RouteTable for Routing url, RouteTable.Routes.MapPageRoute method ask for 3 parameter, that is 'routeName', 'routeUrl' and 'physicalFile' rather than calling Default.aspx, user can call Home/Index.

Now run the application and enter url like 'localhost:/Home/Index' it will automatically invoke Default.aspx page.

routing in mvc

Same way call Home/About-WDI as in the following screenshot,

routing in mvc

Routing in ASP.NET MVC

Routing in MVC is easy, here we have Route.Config.cs file in App_Start Folder, You can define Routes in that file,

By default route is: Home controller - Index Method. 

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

routes.MapRoute has attributes like name, url and defaults like controller name, action and id (optional).

Now let us create one MVC Application and open RouteConfig.cs file and add the following custom route. 

// Custom Route  
routes.MapRoute(  
  name: "about",  
  url: "Home/About-WDI",  
  defaults: new { controller = "Home", action = "About", id = UrlParameter.Optional }  
);

It will invoke 'Home' controller 'About' Action Method.

Routing in ASP.NET MVC 

So for example if user hits url : 'localhost:/Home/About-WDI' it will invoke Home controller 'About' Action Method.

Routing in ASP.NET MVC

Parameter Id is optional here, it can be used while working with id Field like Home/editProduct/3

Enable Attributes Routing

You can also Enable Attributes routing by writing the following code in Route.config.cs file.

//Enabling Attribute Routing   
routes.MapMvcAttributeRoutes(); 

Enable Attributes Routing 

Now rather than writing and managing Routes from Route.config you can manage routes from Controller. As in the following screenshot we have added [Route("Home/ContactUs")] above Contact() ActionResult.

[Route("Home/ContactUs")]  
public ActionResult Contact()  
{  
    ViewBag.Message = "Your contact page.";  
  
    return View();  
}

Attributes Routing 

Now run your application and enter url like: 'localhost:/Home/contactUs' invokes Contact Method.

Enable Attributes Routing

Hope you like this article. Same way if you are working with areas then you can manage the routes from AreaRegistration.cs file.

Please download code files for more details.


Similar Articles