System. Web. Routing

Source: Internet
Author: User

UrlRouting advanced applications
Expected results: Copy codeThe Code is as follows: When I access/a/B. aspx, it will go to Default. aspx? Category = a & action = B 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/, it will go to Default. aspx? Category = chsword & action = index is displayed.
Category: chsword
Action: index

First, create a RouteCopy codeThe Code is as follows: routes. Add (
"Default ",
New Route ("{category}/{action}. aspx ",
New RouteValueDictionary (
New
{
File = "Default ",
Category = "home ",
Action = "index"
}), New MyRouteHandler ()
)
);

Of course, the IHttpHandler processing method must also be changed.
For convenience, I used the following method:Copy codeThe Code is as follows: 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 code in Default. aspx:Copy codeThe Code is as follows: category: <% = Request. Params ["category"] %> <br/>
Action: <% = Request. Params ["action"] %>

To display the input parameters.
If you enter/a/when setting Index. aspx in IIS,/a/index. aspx is accessed. That is, the value set in RouteValueDictionary is automatically completed by default.
UrlRouting uses regular expression rules
UrlRouting can also be defined according to regular rules.Copy codeThe Code is as follows: 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 code above specifies that the action can only be a number, and the access to/a/1. chs can be accessed normally.
When accessing/a/B. chs, the resource is not found.
Of course, you can use a more advanced regular expression.
UrlRouting skills
To exclude UrlRouting:
System. Web. Routing provides an IRouteHandler-StopRoutingHandler Class by default. The URLs processed by this Class will 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.Copy codeThe Code is as follows: public class RouteHandlerFactory: IRouteHandler
{
String Name {get; set ;}
Public RouteHandlerFactory (string name) {this. Name = name ;}
# Region IRouteHandler Member
Public IHttpHandler GetHttpHandler (RequestContext 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.Copy codeThe Code is as follows: 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 );
}

Download Demo code:
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.