In a route, define ({and}) by placing a placeholder in the curly braces. When parsing URLs, the symbols "/" and "." are parsed as a definition, and the values between the definitions match into placeholders. Information in the route definition that is not in curly braces is used as a constant value.
Here are some sample URLs:
Val ID route Definitions |
Examples of matching URL |
{Controller}/{action}/{id} |
/products/show/beverages |
{table}/details.aspx |
/products/details.aspx |
Blo G/{action}/{entry} |
/blog/show/123 |
{Reporttype}/{year}/{mo Nth}/{day} |
/SALES/2008/1/5 |
In general, we add routes to the Application_Start event in the Global.asax file, which ensures that routes is available when the program is started, and also allows you to call the method directly when you are doing unit testing. If you want to call it directly at the time of unit testing, the method that registers the routes must be static with a routecollection parameter.
The following example is the code in Global.asax, which demonstrates adding a route object that contains two URL parameters for action and CategoryName:
public static void RegisterRoutes (RouteCollection routes)
{
Ignore the route to the. axd file, which is the same as WebForm directly to access the. Axd file
Routes. Ignoreroute ("{resource}.axd/{*pathinfo}");
Routes. MapRoute (
"Category",//Name of Route
"Category/{action}/{categoryname}",//URL with parameters
New {controller = "Category", action = "Index", CategoryName = "4MVC"}//Set default parameters
);
}
protected void Application_Start ()
{
Register the route rules that we defined earlier when the program starts
RegisterRoutes (routetable.routes);
}
For more articles, please refer to:
- System.Web.Routing Introductory and advanced next by heavy Pawnage
- System.Web.Routing introductory and advanced by heavy Pawnage
- ASP. NET MVC URL Routing Learning by Q.lee.lulu
- ASP. Routing (Official document)
I'm not going to explain it in detail here. Here are just a few simple explanations.
Ignore routing for a certain type of URL:
Ignore the route to the. axd file, which is the same as WebForm directly to access the. Axd file
Routes. Ignoreroute ("{resource}.axd/{*pathinfo}");Add constraints to support regular expressions. For example, we need to add a condition that must be a number to the ID parameter:
Routes. MapRoute (
"Default",
"{Controller}/{action}/{id}",
New {controller = "Home", action = "Index", id = ""},
New {id = @ "[\d]*"}//id must be a number
);Use an asterisk (*) to match an indeterminate number of parameters, which matches all remaining parameters after the URL. For example:
Query/{queryname}/{*queryvalues}
For url:query/aspnetmvc/preview5/routing, the parameter matching the queryvalues parameter is preview5/routing.
URL matching route is a top-down match based on the order of route definition. For example, we define two route:
public static void RegisterRoutes (RouteCollection routes)
{
Routes. MapRoute (
"Default",//Name of Route
' {controller}/{action}/{id} ',//url with parameter
New {controller = "Home", action = "Index", id = ""}//Set default parameters
);
Routes. MapRoute (
"Post",
"Post/{id}",
New {controller = "Post", action = "Index", id = ""}
);
}I wonder if you see any problem with the two route defined above? I think you can see that the URL will never match the second route, the route named post, because the URL matching the second route can match the first route, and the URL match route is matched from top to bottom based on the order of route definition. So URLs never match a second route. So, when defining the route, put some special route in front of it.
If you are deploying ASP. NET MVC under IIS6, because IIS6 does not have an extension URL for Http://blog.51mvc.com/index, it will not be left to ASP. aspnet_ Isapi.dll, you may have a 404 error when deploying your ASP. NET MVC program to IIS6. You can add a wildcard character to your ASP. NET MVC site:
Then click on the "Insert" button under "Wildcard Application mapping" and set the following in the popup dialog box:
If you are concerned that adding wildcards will give you a performance problem, then you can modify the route to include the extension, which is completely defined by yourself, for example, we use 4MVC to do the URL extension:
Routes. MapRoute (
"Default",//Name of Route
' {controller}.4mvc/{action}/{id} ',//url with parameter
New {controller = "Home", action = "Index", id = ""}//Set default parameters
);Then add a mapping for this extension in IIS6:
Then we visited a URL similar to: Http://blog.51mvc.com/Home.4mvc/index
Some friends on the group said they wanted the tutorials to be written according to an example program, which would make it easier for them to learn. So here is a blog sample program, in order to facilitate the model on the direct use of BlogEngine business entity part. Here we first define the route of this blog:
public static void RegisterRoutes (RouteCollection routes)
{
Ignore the route to the. axd file, which is the same as WebForm directly to access the. Axd file
Routes. Ignoreroute ("{resource}.axd/{*pathinfo}");
Routes. MapRoute (
"Admin",
"Admin/{action}",
New{controller = "Admin", action = "Index"}
);
Routes. MapRoute (
"Postbyid",
"Post/{id}",
New{controller = "Home", action = "Post", id = ""},
New{id = @ "[\d]+}"}
);
Routes. MapRoute (
"Postbyslug",
"Post/{slug}",
New{controller = "Home", action = "Post"}
);
Routes. MapRoute (
"Default",//Name of Route
' {controller}/{action}/{id} ',//url with parameter
New{controller = "Home", action = "Index", id = ""}//Set default parameters
);
} Learn to stay here for the time being. I'll give you the code for this example blog program later. enjoy! Post by Q.lee.lulu.
Go Introduction to ASP. NET MVC 3, Routing