Url Routing in Asp.Net MVC Example with Multiple Parameters (Query Strings)

  Total Views : 6977
  Comments : 0
 
  Keywords : Routing in asp net mvc with example, Understand url routing in asp.net with example, As.net mvc url routing with multiple parameters or query strings, Asp.net mvc url routing overview

Let’s first understand what is url routing in asp.net mvc and how to generate urls in asp.net mvc with example. In asp.net mvc routing module is responsible for mapping incoming browser requests to particular mvc controller actions.

 

Route - The route is a just a URL pattern that is mapped to handler.

 

Routing - The ASP.NET MVC routing is just a pattern matching system. 

 

In asp.net mvc application whenever user send request and if it is a first request then route table will fill first.

 

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

 

In asp.net mvc depending upon the URL request by user UrlRoutingModule will find URL in Route table to create RouteData object. If UrlRoutingModule find correct match then it will create RequestContext and it will send request to appropriate MVCHandler. Once request received then MVCHandler invokes execute method on the Controller. 

 

In asp.net mvc url routing process will happen like the Execute() method will get action method from RouteData based on the requested URL then MVC Controller will call Controller ActionInvoker which creates a list of parameters coming with URL and this parameter list will be passed to controller action method to call InvokeAction method to execute action. Finally it will send response to browser.

Creating Asp.Net MVC Application

we will learn how to create url routing in asp.net mvc with example for that Open visual studio studio à Go to File à Select New à Select Project

 

create new asp.net mvc project from visual studio 2012

 

After that you will see new dialog will pop up for selecting your Template and Project type. From Templates select Visual C# à inside that select Web and then project type select ASP.NET MVC 4 Web Application and here we are giving name as “Tutorial8” then finally click on ok button

 

select asp.net mvc 4 web application and click ok to create new mvc application

 

After naming it just click on OK now new dialog will pop up for selecting template in that Select Empty template and click ok like as shown below 

 

select basic template to create new application in asp.net mvc

 

After adding template our routing example in asp.net mvc project structure like as shown below 

 

Asp.Net MVC Url Routing example

 

After completing creating application now its time to add to Adding Controller.

Adding Controller in Asp.Net MVC

For adding controller just Right click on Controller Folder inside that select Add and then select Controller

 

Adding new controller in asp.net mvc url routing application

 

After selecting Controller a new dialog will popup for asking Controller name and other configuration details. Give Controller ame as “HomeController” and in Template select Empty MVC Controller. After adding controller our project structure like as shown below

 

url routing asp.net mvc application after adding controller

 

Our HomeController will contain code like as shown below 

 

using System;

using System.Collections.Generic;

using System.Linq;

using System.Web;

using System.Web.Mvc;

 

namespace Tutorial8.Controllers

{

public class HomeController : Controller

{

public ActionResult Index()

{

return View();

}

}

}

Adding View in Asp.Net MVC

For Adding View just right click inside Index ActionResult Method and Select "Add View" to create the view template for our Index form. Here in below snapshot we selected View engine as Razor and we are not going to create a strongly type view that's why we didn't check Create option strongly type view finally click on Add button. 

 

add view to controller in url routing asp.net mvc application

 

We have completed adding View now let's move towards Understanding Routing Configuration. Generally the Basic Route which is available whenever we create mvc application. In our application there is App_Start folder inside that you will find RouteConfig.cs. Following is default route which is provide by 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 });

}

}

}

For creating Routes we need to provide (name, URL, object defaults parameters) 

 

routes: A collection of routes for the application.

name: The name of the route to map.

url: The URL pattern for the route.

defaults: An object that contains default route values.

 

routes.MapRoute(

name: "Default",

url: "{controller}/{action}/{id}",

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

Now let’s check out our Home controller and Index view how to Access them

Accessing Home controller and Index page

We first need to provide Controller name then Action Method name after that any optional parameter. In below URL we passed Controller name as "Home" and Action Method name as "Index" and url will be like  "http://localhost:37432/home/index"

 

Here is code which is going to execute when we are going to access this above URL. First thing it will find controller name with Home and then inside that it will find Action Method Name with Index. 

 

using System;

using System.Collections.Generic;

using System.Linq;

using System.Web;

using System.Web.Mvc;

 

namespace Tutorial8.Controllers

{

public class HomeController : Controller

{

public ActionResult Index()

{

return View();

}

}

}

This was a simple Url which is for accessing our View - "http://localhost:37432/home/index". Here is snapshot of assessing Page with following URL "http://localhost:37432/home/index"

 

Output of url routing in asp.net mvc application

Now let’s pass optional parameter and access Index View. Here is URL which we are going to passing with Parameter  "http://localhost:37432/home/index/10"

Create Route to accept with or without parameters

Following routes work for both with parameter and without parameter because parameter optional

 

routes.MapRoute(

name: "Default",

url: "{controller}/{action}/{id}",

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

For getting that parameter value we modified an Index Action Method which now take ID as input parameter and Using Content to Display value on view.

 

using System;

using System.Collections.Generic;

using System.Linq;

using System.Web;

using System.Web.Mvc;

 

namespace Tutorial8.Controllers

{

public class HomeController : Controller

{

public ActionResult Index(string id)

{

return Content(id);

}

}

}

Now let’s run application and check how it works. Here is snapshot of accessing Page with following URL "http://localhost:37432/home/index/10"

 

Output2 of url routing in asp.net mvc application

Below is snapshot in debug mode with passing parameter

 

Debugging mode while passing parameters to url routing in asp.net mvc

 

Create a New Route which takes Multiple Parameters

Here we are trying to send Country and State both as input parameters. For that in RouteConfig.cs we made modification by adding new routes with name Citysearch

 

routes.MapRoute(

"Citysearch",

"Find/{Country}/{state}",

new { controller = "Home", action = " Find", Country = UrlParameter.Optional, state = UrlParameter.Optional }

);

In Home Controller we need to add another action method with name Search that will be like as shown below 

 

public ActionResult Find(string Country, string state)

{

string result = "Country :- " Country " State :- " state;

return Content(result);

}

The Above Route Find/Country/State maps to Controller "Home" and Action Method "Find/Country/State". Below is snapshot in debug mode while passing 2 parameters to Action Method

 

Debugging mode while passing multiple parameters to url routing in asp.net mvc

 

Below is output after passing 2 parameters with Action Method. URL will be "http://localhost:37432/Find/India/Maharashtra"

 

Output of url routing with multiple parameters in asp.net mvc applicationThis is how we can access url routing with or without parameters in asp.net mvc application.

 Comments (0)

Be the first to give your valuable feedback

Leave a comment

  • Default Tutlane User