Asp.Net MVC Routing

  Total Views : 3843
  Comments : 0
 
  Keywords : Routing in asp.net mvc with example, Routing basics in asp.net mvc, Asp.net mvc routing with example, Understand Routing concept in asp.net mvc

Here we will learn how to use url routing in asp.net mvc with example and how to configure url patterns using asp.net mvc routing with example.

Asp.Net MVC Routing Overview

The route is a just a URL pattern that is mapped to handler. In ASP.NET MVC routing is just a pattern matching system whenever user send request to a MVC application and if it is first request then first thing is to fill the route table.

What is Route Table in Asp.Net MVC ?

The Route Table is a class that stores the URL routes for your application.

 

Depending upon the URL request by user using UrlRoutingModule find URL in Route table for creating RouteData object. If UrlRoutingModule find correct match then it goes to create RequestContext and then it forward the request to appropriate MVCHandler. Once MVCHandler receive request it invokes execute method on the Controller.

 

The Execute() method gets the Action from the RouteData based on the requested URL. Then MVC Controller will call Controller ActionInvoker which creates a list of parameters coming with URL this parameter list will be passed to Controller Action method. It calls InvokeAction method to execute action. Finally send response to browser.

Where is routing located in MVC application?

In your application there is App_Start folder inside that you will see RouteConfig.cs it will contain all routing configuration details

How to configure route in MVC?

Following is the default route provided by Asp.Net MVC application

 

using System;

using System.Collections.Generic;

using System.Linq;

using System.Web;

using System.Web.Mvc;

using System.Web.Routing;

 

namespace Tutorial3

{

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

}

}

}

Here we can set your startup page for your application. In place of  home controller you can add your own controller name which ever you have created and related action in place of Index. If you are passing parameters then it will come in { id }. We need to change following code line for custom routing implementation

 

defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional });

In case If you want to increase or pass multiple parameter then you can do it like as shown below 

 

routes.MapRoute(

            "Default",     // Route name

            "{controller}/{action}/{Userid}/{requestID}",  // URL with parameters

            new { controller = "Home", action = "Index", Userid = "", requestID = "" } );

This way we can set url pattern for our asp.net mvc application using routing configuration.

 Comments (0)

Be the first to give your valuable feedback

Leave a comment

  • Default Tutlane User