| Urlrewriter. config namespace Rewriterconfigserializersectionhandler. CS This class returns a deserialization XML document Using system; Using system. configuration; Using system. xml; Using system. xml. serialization; Using system. xml. XPath; Namespace urlrewriter. config { Public class rewriterconfigserializersectionhandler: iconfigurationsectionhandler {
Public object create (Object parent, object configcontext, system. xml. xmlnode Section) { // Create an instance of xmlserializer Based on the rewriterconfiguration type... Xmlserializer SER = new xmlserializer (typeof (rewriterconfiguration )); // Return the deserialized object from the Web. config XML Return Ser. deserialize (New xmlnodereader (section )); } } } Rewriterconfiguration. CS Return configuration node Information Using system; Using system. Web; Using system. Web. caching; Using system. configuration; Using system. xml. serialization; Namespace urlrewriter. config { [Serializable ()] [Xmlroot ("rewriterconfig")] Public class rewriterconfiguration { // Private member variables Private rewriterrulecollection rules; // an instance of Public static rewriterconfiguration getconfig () { If (httpcontext. Current. cache ["rewriterconfig"] = NULL) Httpcontext. Current. cache. insert ("rewriterconfig", configurationsettings. getconfig ("rewriterconfig ")); Return (rewriterconfiguration) httpcontext. Current. cache ["rewriterconfig"]; } # Region public properties /// <Summary> /// A <see CREF = "rewriterrulecollection"/> instance that provides access to a set of <see CREF = "rewriterrule"/> S. /// </Summary> Public rewriterrulecollection rules { Get { Return rules; } Set { Rules = value; } } # Endregion } } Rewriterrulecollection. CS Method Using system; Using system. collections; Namespace urlrewriter. config { [Serializable ()] Public class rewriterrulecollection: collectionbase { /// <Summary> /// Adds a new rewriterrule to the collection. /// </Summary> /// <Param name = "R"> A rewriterrule instance. </param> Public Virtual void add (rewriterrule R) { This. innerlist. Add (R ); } /// <Summary> /// Gets or sets a rewriterrule at a specified ordinal index. /// </Summary> Public rewriterrule this [int Index] { Get { Return (rewriterrule) This. innerlist [Index]; } Set { This. innerlist [Index] = value; } } } } Rewriterrule. CS Defined attributes Using system; Namespace urlrewriter. config { Public class rewriterrule { Private string lookfor, sendto; Public String lookfor { Get { Return lookfor; } Set { Lookfor = value; } } Public String sendto { Get { Return sendto; } Set { Sendto = value; } }
} } Urlrewriter namespace In this example, the preceding rules is processed. Basemodulerewriter. CS Initialization and Loading Method Using system; Using system. Web; Namespace urlrewriter { Public abstract class basemodulerewriter: ihttpmodule {
Public Virtual void Init (httpapplication APP) {
App. authorizerequest + = new eventhandler (this. basemodulerewriter_authorizerequest ); } Public Virtual void dispose (){} Protected virtual void basemodulerewriter_authorizerequest (Object sender, eventargs E) { Httpapplication APP = (httpapplication) sender; Rewrite (App. Request. Path, APP ); } Protected abstract void rewrite (string requestedpath, httpapplication APP ); } } Modulerewriter. CS Loop out each rule and execute rewriteurl Using system; Using system. Text. regularexpressions; Using system. configuration; Using urlrewriter. config; Namespace urlrewriter { /// <Summary> /// Provides a rewriting httpmodule. /// </Summary> Public class modulerewriter: basemodulerewriter { /// <Summary> /// This method is called during the module's beginrequest event. /// </Summary> /// <Param name = "requestedrawurl"> the rawurl being requested (required des path and querystring). </param> /// <Param name = "app"> the httpapplication instance. </param> Protected override void rewrite (string requestedpath, system. Web. httpapplication APP) { // Log information to the trace object. // App. Context. Trace. Write ("modulerewriter", "entering modulerewriter "); // Get the configuration rules Rewriterrulecollection rules = rewriterconfiguration. getconfig (). Rules; // Iterate through each rule... For (INT I = 0; I <rules. Count; I ++) { // Get the pattern to look for, and resolve the URL (convert ~ Into the appropriate directory) String lookfor = "^" + rewriterutils. resolveurl (App. Context. Request. applicationpath, rules [I]. lookfor) + "$ "; // Create a RegEx (note that ignorecase is set ...) RegEx Re = new RegEx (lookfor, regexoptions. ignorecase ); // See if a match is found If (Re. ismatch (requestedpath )) { // Match found-Do any replacement needed String sendtourl = rewriterutils. resolveurl (App. Context. Request. applicationpath, re. Replace (requestedpath, rules [I]. sendto )); // Log rewriting information to the trace object App. Context. Trace. Write ("modulerewriter", "Rewriting URL to" + sendtourl ); // Rewrite the URL Rewriterutils. rewriteurl (App. Context, sendtourl ); Break; // exit the For Loop } } // Log information to the trace object App. Context. Trace. Write ("modulerewriter", "exiting modulerewriter "); } } } Rewriterutils. CS Convert the URL Based on the specified path and perform rewritepath We can see that the last step is to execute the context. rewritepath (PATH) method. In httphandle, context. server. Transfer (URL) is used) Using system; Using system. Web; Namespace urlrewriter { /// <Summary> /// Provides utility helper methods for the rewriting httpmodule and httphandler. /// </Summary> /// <Remarks> this class is marked as internal, meaning only classes in the same assembly will be /// Able to access its methods. </remarks> Internal class rewriterutils { # Region rewriteurl /// <Summary> /// Rewrite's a URL using <B> httpcontext. rewriteurl () </B>. /// </Summary> /// <Param name = "context"> the httpcontext object to rewrite the URL to. </param> /// <Param name = "sendtourl"> the URL to rewrite to. </param> Internal static void rewriteurl (httpcontext context, string sendtourl) { String X, Y; Rewriteurl (context, sendtourl, out X, out y ); } /// <Summary> /// Rewrite's a URL using <B> httpcontext. rewriteurl () </B>. /// </Summary> /// <Param name = "context"> the httpcontext object to rewrite the URL to. </param> /// <Param name = "sendtourl"> the URL to rewrite to. </param> /// <Param name = "sendtourllessqstring"> returns the value of sendtourl stripped of the querystring. </param> /// <Param name = "filepath"> returns the physical file path to the requested page. </param> Internal static void rewriteurl (httpcontext context, string sendtourl, out string sendtourllessqstring, out string filepath) { // See if we need to add any extra querystring Information If (context. Request. querystring. Count> 0) { If (sendtourl. indexof ('? ')! =-1) Sendtourl + = "&" + context. Request. querystring. tostring (); Else Sendtourl + = "? "+ Context. Request. querystring. tostring (); } // First strip the querystring, if any String querystring = string. empty; Sendtourllessqstring = sendtourl; If (sendtourl. indexof ('? ')> 0) { Sendtourllessqstring = sendtourl. substring (0, sendtourl. indexof ('? ')); Querystring = sendtourl. substring (sendtourl. indexof ('? ') + 1 ); } // Grab the file's physical path Filepath = string. empty; Filepath = context. server. mappath (sendtourllessqstring ); // Rewrite the path... Context. rewritepath (sendtourllessqstring, String. Empty, querystring ); // Note! The above rewritepath () overload is only supported in the. NET Framework 1.1 // If you are using. NET Framework 1.0, use the below form instead: // Context. rewritepath (sendtourl ); } # Endregion /// <Summary> /// Converts a URL into one that is usable on the requesting client. /// </Summary> /// <Remarks> converts ~ To the requesting Application Path. mimics the behavior of /// <B> control. resolveurl () </B> method, which is often used by control developers. </remarks> /// <Param name = "apppath"> the Application Path. </param> /// <Param name = "url"> the URL, which might contain ~. </Param> /// <Returns> A resolved URL. If the input parameter <B> URL </B> contains ~, It is replaced with /// Value of the <B> apppath </B> parameter. </returns> Internal static string resolveurl (string apppath, string URL) { If (URL. Length = 0 | URL [0]! = '~ ') Return URL; // There is no ~ In the first character position, just return the URL Else { If (URL. Length = 1) Return apppath; // There is just ~ In the URL, return the apppath If (URL [1] = '/' | URL [1] = '\\') { // URL looks like ~ /Or ~ \ If (apppath. length> 1) Return apppath + "/" + URL. substring (2 ); Else Return "/" + URL. substring (2 ); } Else { // URL looks like ~ Something If (apppath. length> 1) Return apppath + "/" + URL. substring (1 ); Else Return apppath + URL. substring (1 ); } } } } } The above is just a humble opinion, and errors are inevitable. |