Custom URL rewriting 04.18

Source: Internet
Author: User

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.

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.