System. Web. Routing

Source: Internet
Author: User

 

System. Web. Routing

The above is the simplest definition method. Of course, we can create more complex rules. This includes setting the default value of the rule and setting the regular expression of the rule.

Urlrouting Advanced Applications

Expected results:

When I access/a/B. aspx, it will go to default. aspx? Category = A & Action = B is displayed on the page
Category:
Action: B

If I access/chsword/xxxx. aspx, it will be transferred to default. aspx? Category=Chsword & Action=XXXX is displayed.
Category: chsword

Action: xxxx

 

If you access/chsword /,Default. aspx? Category=Chsword & Action=The index is displayed.

 Category: chsword

Action: Index

 

 First, createRoute

Routes. Add (
" Default " ,
New Route ( " {Category}/{action}. aspx " ,
New Routevaluedictionary (
New
{
File =   " Default " ,
Category =   " Home " ,
Action =   " Index "
}), New Myroutehandler ()
)
);

Of courseIhttphandlerThe processing method must also be changed.

For convenience, I used the following method:

 

Context. server. Execute ( String . Format ( " /{0}. aspx? Category = {1} & Action = {2} " ,
Requestcontext. routedata. Values. containskey ( " File " )
? Requestcontext. routedata. Values [ " File " ]. Tostring ()
: " Default " ,
Requestcontext. routedata. Values. containskey ( " Category " )
? Requestcontext. routedata. Values [ " Category " ]. Tostring ()
: "" ,
Requestcontext. routedata. Values. containskey ( " Action " )
? Requestcontext. routedata. Values [ " Action " ]. Tostring ()
: "" )
);

 

That is,/a/B. aspx is mapped to default. aspx? Category = A & Action = B

Write the following in default. aspx:Code:

Category: <% = Request. Params [ " Category " ] %> < BR />
Action: <% = Request. Params [ " Action " ] %>

 

To display the input parameters.

IfIISSet inIndex. aspxInput//Will also access/A/index. aspxBy defaultRoutevaluedictionaryThe value set in

Urlrouting Use regular expression rules

UrlroutingYou can also define them according to regular rules.

 

 

Routes. Add (
" Zz " ,
New Route ( " {Category}/{action}. CHS " ,
New Routevaluedictionary (
New {
File =   " Default " ,
Category =   " Home " ,
Action =   " 1 "
}),
New Routevaluedictionary (
New {
Action =   " [\ D] + "
}),
New Myroutehandler ()
)
);

 

The above Code specifiesActionOnly numbers are allowed for access/A/1.chsNormal access.

Access/A/B. CHSNo resource is found.

 

Of course, you can use a more advanced regular expression.

Urlrouting Skills Exclude Urlrouting Method:

System. Web. RoutingBy default,Iroutehandler-Stoproutinghandler class, After processingURLWill not be processed

The general usage is as follows:

Routes.Add (New Route("{Resource}. axd/{* pathinfo }",New Stoproutinghandler()));

 

Routehandler Factory:

In fact, iroutehandler can implement a simple factory of routehandler.

Public   Class Routehandlerfactory: iroutehandler
{
String Name { Get ; Set ;}
Public Routehandlerfactory ( String Name ){ This . Name = Name ;}
# Region Iroutehandler Member
Public Ihttphandler gethttphandler (requestcontext ){
If ( This . Name =   " Mypage " )
Return   New Mypage (requestcontext );
If ( This . Name = " Mypage1 " )
Return   New Mypage1 (requestcontext );
}
# Endregion
}

 

HTTP verbs is required. Use httpmethodconstraint in system. Web. routing.

 

Void Application_start ( Object Sender, eventargs e ){
Registerroutes (routetable. routes );
}
Public   Static   Void Registerroutes (routecollection routes ){
String [] Allowedmethods = { " Get " , " Post " };
Httpmethodconstraint methodconstraints =   New Httpmethodconstraint (allowedmethods );
Route reportroute =   New Route ( " {Locale}/{year} " , New Reportroutehandler ());
Reportroute. Constraints =   New Routevaluedictionary {{ " Httpmethod " , Methodconstraints }};
Routes. Add (reportroute );
}

 

DemoProgramDownload Code:

Http://files.cnblogs.com/chsword/WebApplication3.rar

 

 

 

 

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.