MVC - URL Routing



 The segments in an examle URL

The first segment contains the word Admin, and the second segment contains the word Index. To the
human eye, it is obvious that the first segment relates to the controller and the second segment relates to the action. But, of course, we need to express this relationship in a way that the routing system can
understand. Here is a URL pattern that does this:

{controller}/{action}
or
{controller}/{action}/id
or
{controller}/{action}/id?querystring2=&querystring3=

 Note : id, querystring2, and querystring 3 is a parameter in the controller.

When processing an incoming request, the job of the routing system is to match the URL that has
been requested to a pattern and extract values from the URL for the segment variables defined in the
pattern. The segment variables are expressed using braces (the { and } characters). The example pattern has two segment variables with the names controller and action, and so the value of the controller segment variable will be Admin and the value of the action segment variable will be Index.

Customize the routing rules that MVC uses to locate an action.

App_Start >> RouteCinfig.cs

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

Make Routing support url like :

/Admin/Index/id/querystring2/querystring3

 
routes.MapRoute(
            name : "Default", 
            url : "{controller}/{action}/{id}/{querystring2}/querystring3",
            defaults : new { controller = "Home", action = "Index",
  id= UrlParameter.Optional,
  querystring2=UrlParameter.Optional, 
querystring3 =UrlParameter.Optional }
);

By NOTE : – If You have Found this post Helpful, I will appreciate if you can Share it on Facebook, Twitter and Other Social Media Sites. Thanks =)

Popular posts from this blog

Example to disable save as certain file type in SSRS Report Viewer

How to create DataGrid or GridView in JSP - Servlet

Control Webpart Visible/Enable using macro in Kentico