Routedebug. dll, an ASP. net mvc route matching detection component

Source: Internet
Author: User

Previously, routemonitor. dll was used for MVC routing to detect URL path ing matching. Because the company's computer does not have this component, I searched the internet and found that routemonitor. dll has changed its name to routedebug. dll. For more information, see the official website. : Http://files.cnblogs.com/Capricornus/RouteDebug-Binary.zip

 

Usage:

1. add reference to this component in the MVC Project

2. Global ApplicationProgramSet in global. asax. CSCode

 
Protected void application_start () {registerroutes (routetable. routes); routedebug. routedebugger. rewriteroutesfortesting (routetable. routes); // previous routemonitor method // routemonitor. routedebugger. rewriteroutesfortesting (routetable. routes );}

3. Matched routes include:

We can use reflector to decompile this routedebugger. dll component and check the principle.

Routedebug contains four classes: debughttphandler, debugroute, debugroutehandler, and routedebugger.

First, start by calling routedebug. routedebugger. rewriteroutesfortesting.

Routedebugger class:

 
Public static class routedebugger {// Methods public static void rewriteroutesfortesting (routecollection routes) {using (routes. getreadlock () {bool flag = false; foreach (routebase base2 in routes) {route = base2 as route; If (route! = NULL) {route. routehandler = new debugroutehandler ();} If (route = debugroute. Singleton) {flag = true ;}} if (! Flag) {routes. Add (debugroute. Singleton );}}}}

First, the entire Code uses system. web. routecollection in the routing namespace. getreadlock () is locked and provides an object to manage the thread security when retrieving objects from the collection. It then traverses the route set parameters we passed in. Replace the original routehandler with the debugroutehandler in routedebug to change the direction of the HTTP handler, and then add the value of the singletion attribute to the routing.

Debugroute class:

 
Public class debugroute: Route {Private Static debugroute Singleton = new debugroute (); Private debugroute (): Base ("{* catchall}", new debugroutehandler ()) {} public static debugroute Singleton {get {return Singleton ;}}}

Debugroute inherits from the route class. The constructor constructs a route that can capture all URL addresses.

Debugroutehandler route handler class:

Public class debugroutehandler: iroutehandler {// Methods public ihttphandler gethttphandler (requestcontext) {handler = new handler (); handler. requestcontext = requestcontext; return handler ;}}

Implements the instantiation object of the ihttphanlder interface and passes in a requestcontext object instance.

Debughttphandler class:

Public class debughttphandler: ihttphandler {[compilergenerated] private requestcontext <requestcontext> K _ backingfield; Private Static string formatroutevaluedictionary (routevaluedictionary values) {If (values = NULL) | (values. count = 0) {return "(null)";} string STR = string. empty; foreach (string str2 in values. keys) {STR = STR + String. format ("{0 }={ 1},", str2, values [str2]);} If (Str. endswith (",") {STR = Str. substring (0, str. length-2);} return STR;} public void processrequest (httpcontext context) {string STR = string. empty; If (context. request. querystring. count> 0) {routevaluedictionary dictionary = new routevaluedictionary (); foreach (string str2 in context. request. querystring. keys) {dictionary. add (str2, context. request. querystring [str2]);} virtualpathdata vi Rtualpath = routetable. routes. getvirtualpath (this. requestcontext, dictionary); If (virtualpath! = NULL) {STR = "<p> <label> generated URL </label>:"; STR = STR + "<strong style = \" color: # 00a; \ ">" + virtualpath. virtualpath + "</strong>"; route = virtualpath. route as route; If (route! = NULL) {STR = STR + "using the route \" "+ route. URL + "\" </P> ";}}} string format = "<HTML> \ r \ n 

The processrequest method is used to process the request, and is finally displayed on the routing detection page.

First, you can obtain the routedata class from requestcontext. routedata. The routedata class contains the value of the requested route. Obtain the URL parameter value and default value set of the route from routedata. Values, obtain the route object from routedata. route, and obtain the route information that matches the specified value in the set.

 

Related Article

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.