System.Web.Routing introduction and progression

  • 2020-05-16 06:43:12
  • OfStack

UrlRouting advanced applications
Expected effect:
 
 When I visit /a/b.aspx It will be transferred to Default.aspx?category=a&action=b Display on the page  
category:a 
action:b 
 Also if I visit /chsword/xxxx.aspx You will get the Default.aspx?category=chsword&action=xxxx Will be displayed  
category:chsword 
action:xxxx 
 If you visit /chsword/ You will get the  Default.aspx?category=chsword&action=index Will be displayed  
category:chsword 
action:index 

First I set up an Route
 
routes.Add( 
"Default", 
new Route("{category}/{action}.aspx", 
new RouteValueDictionary( 
new 
{ 
file = "Default", 
category = "home", 
action = "index" 
}), new MyRouteHandler() 
) 
); 

Of course, the way IHttpHandler is handled should also be changed
In order to facilitate the review, I used the following methods:
 
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() 
: "") 
); 

So /a/ b.aspx is mapped to Default.aspx, right? category = a & action = b
Write the following code in Default.aspx:
 
category:<%=Request.Params["category"] %><br /> 
action:<%=Request.Params["action"] %> 

To display the parameters passed in.
If you set Index.aspx in IIS, even if you type /a/, you will be able to access /a/ index.aspx, which means that the default will be automatically completed according to the values set in RouteValueDictionary
UrlRouting USES regular expression rules
When UrlRouting is defined, it can also be defined 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 stipulates that if action can only be Numbers, access /a/ 1.chs can be accessed normally.
Access to /a/ b.chs will show that no resources were found.
Of course, this is where you can use more advanced regular expressions.
The technique of UrlRouting
Methods to exclude UrlRouting:
System.Web.Routing provides one IRouteHandler-StopRoutingHandler Class by default, and URL processed by it will not be processed
It is usually used 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  Members of the  
public IHttpHandler GetHttpHandler(RequestContext requestContext) { 
if (this.Name == "mypage") 
return new MyPage(requestContext); 
if(this.Name="mypage1") 
return new MyPage1(requestContext); 
} 
#endregion 
} 

HTTP verbs, 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); 
} 

Demo program code download:
WebApplication3.rar

Related articles: