Use route in ASP. NET webform

Source: Internet
Author: User

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

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.