1. Define XML rules and configure URL nodes
? XML version = "1.0" encoding = "UTF-8"?> URLs > rewrite name =" shownews " pattern =" news-(\ d00000000.html " page =" News. aspx " query =" id = $1 " > rewrite > URLs >
2. parse XML and encapsulate it
Public Class Myurlrewrite { Public String Name { Get ; Set ;} Public String Pattern { Get ; Set ;} Public String Page { Get ; Set ;} Public String Query { Get ; Set ;}} Public Class Urllist { Private Static Urllist instance; Public List <myurlrewrite> Getallurls { Get ; Set ;} Private Urllist () {getallurls = New List <myurlrewrite> (); Xmldocument xmldoc = New Xmldocument (); String Xmlpath = httpcontext. Current. Request. physicalapplicationpath + " /Urllink. config " ; Xmldoc. Load (xmlpath); xmlnode Node = Xmldoc. selectsinglenode ( " URLs " ); Foreach (Xmlnode item In Node. childnodes) {xmlattribute name = Item. attributes [ " Name " ]; Xmlattribute Pattern = Item. attributes [ " Pattern " ]; Xmlattribute page = Item. attributes [ " Page " ]; Xmlattribute Query = Item. attributes [ " Query " ]; If (Name = Null | Pattern = Null | Query = Null | Pattern = Null ) Continue ; Myurlrewrite Model =New Myurlrewrite (); Model. Name = Name. value; model. Pattern = Pattern. value; model. Page = Page. value; model. Query = Query. value; getallurls. Add (model );}} Public Static Urllist geturllist (){ If (Instance = Null ) {Instance = New Urllist ();} Return Instance ;}}
3. Customize the ihttpmodule interface to implement URL rewriting. [I will not talk about this interface more, but there are a lot on the Internet]
/* ** Provide module initialization and event processing for implementation Classes * */ Public Class Myhttpmodule: system. Web. ihttpmodule { # Region Ihttpmodule Member // Dispose of resources used by modules implementing ihttpmodule (excluding memory ). Public Void Dispose (){ Throw New Notimplementedexception ();} // Initialize the module and make it ready to process requests. Public Void Init (httpapplication context) {context. beginrequest + = New Eventhandler (context_beginrequest );} Void Context_beginrequest ( Object Sender, eventargs e) {httpcontext Context = (Httpapplication) sender). context; String Requestpath = Context. Request. Path. tolower (); Foreach (Myurlrewrite item In Urllist. geturllist (). getallurls ){ // Not distinguished by sizeWrite If (RegEx. ismatch (requestpath, item. pattern, regexoptions. ignorecase )){ String Newurl = RegEx. Replace (requestpath, item. pattern, item. query ); If (Newurl. Length = 0 ) Context. rewritepath (item. Page ); Else Context. rewritepath (item. Page + " ? " + Newurl. substring ( 1 ));}}} # Endregion }
4. Configure web. config
<Httpmodules><AddName= "Urlrewrite"Type= "_ 017_url rewrite. myhttpmodule, 017_url Rewrite"/></Httpmodules>
5. Final
Summary:
1. It is important to understand the pipelines and lifecycles of Asp.net.
2. SetProgramConfigurable, avoid always modifying the demo, and use XML to define rules
3. The most important thing is that the Lakers have entered the playoffs, but kobe is injured.