Webforms uses system. Web. Routing

Source: Internet
Author: User

Lao Zhao wrote about using ASP in Web application development. net MVC Framework practice skills, routing can now be used as a replacement for urlrewriting technology, appeared in the Asp.net MVC framework, it is very easy to apply it to webforms, you can download ASP on codeplex. net MVC webformrouting demo.

The implementation principle is also very simple:

1. Create a custom iroutehandler to instantiate your page

1:Public ClassWebformroutehandler: iroutehandler {

 
2:PublicWebformroutehandler (StringVirtualpath)
 
3::This(Virtualpath,True){
 
4:}
 
5: 
6:PublicWebformroutehandler (StringVirtualpath,BoolCheckphysicalurlaccess ){
 
7:If(Virtualpath =Null){
 
8:Throw NewArgumentnullexception ("Virtualpath");
 
9:}
 
10: 
 
11:If(! Virtualpath. startswith ("~ /")){
12:Throw NewArgumentexception ("Virtualpath must start with a Tilde slash :\"~ /\"","Virtualpath");
 
13:}
 
14: 
 
15:This. Virtualpath = virtualpath;
 
16:This. Checkphysicalurlaccess = checkphysicalurlaccess;
 
17:}
 
18: 
 
19:/// <Summary>
20:/// This is the full virtual path (using Tilde syntax) to the webform page.
 
21:/// </Summary>
 
22:/// <Remarks>
 
23:/// Needs to be thread safe so this is only settable via ctor.
 
24:/// </Remarks>
 
25:Public StringVirtualpath {Get;PrivateSet ;}
 
26: 
27:/// <Summary>
 
28:/// Because we're not actually rewriting the URL, ASP. NET's URL auth will apply
 
29:/// To the incoming request URL and not the URL of the physical webform page.
 
30:/// Setting this to true (default) will apply URL access rules against
 
31:/// Physical file.
 
32:/// </Summary>
 
33:/// <Value> true by default </value>
34:Public BoolCheckphysicalurlaccess {Get; set ;}
 
35: 
 
36:PublicIhttphandler gethttphandler (requestcontext ){
 
37:StringVirtualpath = getsubstitutedvirtualpath (requestcontext );
 
38:If(This. Checkphysicalurlaccess &&! Urlauthorizationmodule. checkurlaccessforprincipal (virtualpath, requestcontext. httpcontext. User, requestcontext. httpcontext. Request. httpmethod ))
39:Throw NewSecurityexception ();
 
40: 
 
41:VaR page = buildmanager. createinstancefromvirtualpath (virtualpath,Typeof(Page ))AsIhttphandler;
 
42:If(Page! =Null){
 
43:// Pages that don't implement iroutablepage won't have the requestcontext
 
44:// Available to them. Can't generate outgoing routing URLs without that context.
45:VaR routablepage = pageAsIroutablepage;
 
46:If(Routablepage! =Null)
 
47:Routablepage. requestcontext = requestcontext;
 
48:}
 
49:ReturnPage;
 
50:}
 
51: 
 
52:/// <Summary>
53:/// Gets the virtual path to the resource after applying substitutions Based on Route data.
 
54:/// </Summary>
 
55:/// <Param name = "requestcontext"> </param>
 
56:/// <Returns> </returns>
 
57:Public StringGetsubstitutedvirtualpath (requestcontext ){
 
58:If(! Virtualpath. Contains ("{"))
59:ReturnVirtualpath;
 
60: 
 
61:// Trim off ~ /
 
62:StringVirtualpath = virtualpath. substring (2 );
 
63: 
 
64:Route route =NewRoute (virtualpath,This);
 
65:Virtualpathdata VPD = route. getvirtualpath (requestcontext, requestcontext. routedata. values );
 
66:If(VPD =Null)
67:ReturnVirtualpath;
 
68:Return "~ /"+ VPD. virtualpath;
 
69:}
 
70:}

2. Use the custom iroutehandler to register a new routes

 
1:Public ClassGlobal: system. Web. httpapplication
 
2:{
 
3: 
 
4:Protected VoidApplication_start (ObjectSender, eventargs E)
5:{
 
6:Registerroutes (routetable. routes );
 
7:}
 
8: 
 
9:Public Static VoidRegisterroutes (routecollection routes)
 
10:{
 
11:// We are intentionally creating this backdoor as a demonstration
 
12:// Bad security practices.
13:Routes. mapwebformroute ("Secret","Backdoor","~ /Admin/secretpage. aspx",False);
 
14:Routes. mapwebformroute ("Blocked","Frontdoor","~ /Admin/secretpage. aspx",True);
 
15: 
 
16:// Even though we are not checking physical URL access in this route, it shoshould still block because the incoming
 
17:// Request URL wocould start with/admin.
18:Routes. mapwebformroute ("Admin","Admin/{* anything }","~ /Admin/secretpage. aspx",False);
 
19: 
 
20:Routes. mapwebformroute ("Named","Foo/Bar","~ /Forms/Blech. aspx");
 
21:Routes. mapwebformroute ("Numbers","One/two/three","~ /Forms/Haha. aspx");
 
22:
 
23:// Maps any requests for/Haha/*. aspx to/Forms/hahah. aspx
24:Routes. mapwebformroute ("Substitution","Haha/{filename }","~ /Forms/Haha. aspx");
 
25:}
 
26:}
 
 

Msdn magazine article:
Using ASP. NET web forms routing: http://msdn.microsoft.com/zh-cn/magazine/2009.01.extremeaspnet.aspx

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.