Those who have used ASP. net mvc may already have some contact with route. in ASP. NET webform, URL rewrite may be used to implement similar functions. This article describes how to use route in ASP. NET webform.
1. Find the global. asax file. If this file is not found, create a new one.
2. Add the registerroutes Method
Code
1 Public Static Void Registerroutes (routecollection routes)
2 {
3 // Renewal page
4 Routes. mappageroute (
5 " Defautl " ,
6 "" ,
7 " ~ /Folder1/webform1.aspx "
8 );
9
10 // Route URLs in the form of {folder}/{webform}
11 Routes. mappageroute (
12 " Webform1 " ,
13 " {Folder}/{webform} " ,
14 " ~ /{Folder}/{webform}. aspx "
15 );
16
17 // Route URLs in the form of {folder}/{page} (with parameters)
18 Routes. mappageroute (
19 " Webform2 " ,
20 " {Floder}/{webform}/{parameter} " ,
21 " ~ /{Floder}/{webform}. aspx "
22 );
23
24 }
3. Use the registerroute method in the application_start method.
1 Protected Void Application_start ( Object Sender, eventargs E)
2 {
3 Registerroutes (routetable. routes );
4 }
To obtain parameters in the URL, refer to the followingCode
1 String Parameter = Page. routedata. Values [ " Parameter " ] As String ;
It obtains the parameter with the placeholder parameter. For example ~ The parameter value of/folder2/webform3/ABC can be obtained, but if ~ /Folder2/webform3? Parameter = ABC does not seem to get the parameter value of parameter. Available
1 String Parameter = Request. Params [ " Parameter " ] As String ;
.
Sample download
This article applies to ASP. NET 4