ASP. NET pseudo-static implementation

Source: Internet
Author: User

1. What is pseudo-static? What is the role of pseudo-static?

Definition:Dynamic Web Pages remove the parameters of dynamic web pages by rewriting URLs, but there is no need to rewrite pages in the actual web directory.

For example:What is the access address? Year = 2012 & month = 06 & day = 05 & id = 2529259.

Why?

(1) Enhance the friendliness of URLs to facilitate users' memory of URLs.

(2) improve search engine crawling. Many search engines prefer static html pages.

(3) enhanced security because the "year", "month", "day", and "ID" parameters are hidden, making the website less vulnerable to attacks.

 

2. Basic Ideas for pseudo-static implementation

(1) customize the httphandler class and implement the ihttphandler Interface

(2) obtain the URL Information requested by the user

(3) define multiple regular expression rules to match URL strings

(4) Redirect real URL Information

 

3. Custom urlrewriter class

Principle:Use the ihttphandler interface to take over HTTP requests.

(1) define the urlrewriter. CS class

 1   Using  System;  2   Using  System. Collections. Generic;  3   Using  System. LINQ;  4   Using  System. Web;  5  Using  System. text;  6   Using  System. Text. regularexpressions;  7   8   ///   <Summary>  9   ///  Urlrewriter pseudo-static URL rewriting  10   ///   </Summary>  11  Public   Class  Urlrewriter: ihttphandler  12   {  13       ///   <Summary>  14       ///  Custom httphandler  15       ///   </Summary>  16       ///  <Param name = "context"> </param>  17       Public   Void  Processrequest (httpcontext context)  18   {  19           Try  20   {  21               String Url = context. Request. rawurl; //  Obtain the URL Information requested by the user 22 RegEx Reg = New RegEx ( @"  /Detail-(\ D +) \... +  " , Regexoptions. ignorecase ); //  Create a regular expression  23 Match m = reg. Match (URL, URL. lastindexof ( "  /  " )); //  Use regular expressions for URL strings  24              If (M. Success) //  Matched  25   {  26                   String Realpath = @"  ~ /Admin/detail. aspx? Type =  " + M. Groups [ 1 ] + "  & Amp; id =  " + M. Groups [ 2 ];//  Redirect real address information  27   Context. server. Execute (realpath );  28   }  29               Else  30   {  31   Context. response. Redirect (context. Request. url. tostring ());  32   }  33  34   }  35           Catch  (Exception ex)  36   {  37   Context. response. Redirect (context. Request. url. tostring ());  38   }  39   }  40   41       ///  <Summary>    42       ///  True if the system. Web. ihttphandler instance can be used again; otherwise, false.  43       ///   </Summary>    44       Public   Bool  Isreusable  45   {  46           Get { Return  False  ;}  47   }  48 }

 (2) Add configuration information under the <system. Web> node in Web. config

 <?  XML version = "1.0"  ?>  <  Configuration  >  <  System. Web  >    < Httphandlers  >      <! --  Use the custom urlrewriter class  -->      <  Add  Verb  = "*"  Path  = "*/Detail -? *-? *. Html"  Type  = "Urlrewriter"  />      </  Httphandlers >      <  Compilation  Debug  = "True"  Targetframework  = "4.0"  />    </  System. Web  >  </  Configuration  > 

Explanation:

Verb attribute: ProcessingProgramSupported HTTP actions. If a processing program supports all HTTP actions, use "*". Otherwise, use a comma-separated list to list supported actions. Therefore, if your handler only supports HTTP "get" and "Post", the verb attribute should be "get" and "Post ".

Path attribute: Specifies the path and file name of the handler to be called (which can contain wildcards ). All files with the suffix "*. html" are called by the handler. The path attribute should be "*. html ".

Type attribute: indicates the bound class name and namespace (if any ). When ASP. NET is running, search for the part DLL in the bin directory of the application, and then search in the global part buffer (GAC.

(3) Open the website input http: // localhost: 28053/pseudo static/detail-1-1.html, the display is "admin/detail. aspx? Type = 1 & id = 1. (Download the instance inArticleLast)

 

4. Use Microsoft's urlrewriter. dll

(1) Add urlrewriter. dll reference

(2) Configure basic web. config information

 <?  XML version = "1.0"  ?> <  Configuration  >    <! --  Use urlrewriter. dll  -->  <  Configsections  >    <  Section  Name  = "Rewriterconfig"  Requirepermission  = "False"  Type = "Urlrewriter. config. rewriterconfigserializersectionhandler, urlrewriter"   />  </  Configsections  >  <  Rewriterconfig  >    <  Rules  >      <  Rewriterrule  >        <  Lookfor > ~ /Detail/([0-9] *)/([0-9] * ).html </  Lookfor  >        <  Sendto  > ~ /Admin/detail. aspx? Type = $1 & Amp; Id = $2 </  Sendto  >      </  Rewriterrule  >    </ Rules  >  </  Rewriterconfig  >  <  System. Web  >    <  Httphandlers  >      <! --  Use urlrewriter. dll  -->    <  Add  Verb = "*"  Path  = "*. Aspx"  Type  = "Urlrewriter. rewriterfactoryhandler, urlrewriter"   />    <  Add  Verb  = "*"  Path  = "*. Html"  Type  = "Urlrewriter. rewriterfactoryhandler, urlrewriter"   />  </ Httphandlers  >      <  Compilation  Debug  = "True"  Targetframework  = "4.0"  />    </  System. Web  >  </  Configuration  > 

Brief Introduction

 < Rewriterconfig  >     <  Rules  >     <  Rewriterrule  >        <  Lookfor  > Mode to be searched </  Lookfor  >        <  Sendto  > String to be used to replace the pattern </  Sendto  >     </  Rewriterrule  >     <  Rewriterrule  >        <  Lookfor  > Mode to be searched </  Lookfor  >        < Sendto  > String to be used to replace the pattern </  Sendto  >     </  Rewriterrule  >     </  Rules  >  </  Rewriterconfig  > 

Use reflector to view urlrewriter. dll. The Node information is defined in the class and cannot be changed.

(3) Similarly, open the website and enter http: // localhost: 28053/pseudo static/detail-1-1.html, the display is "admin/detail. aspx? Type = 1 & id = 1.

 

5. References

(1) URL rewriting in ASP. NET

(2) Implement pseudo-static Web pages using ASP. NET

(3) iis7 pseudo-static Web. config configuration method

(4) ASP. NET pseudo-static page implementation and pseudo-static configuration in iis7.0

 

 Download instance

 

Author: forevernome
Source: http://www.cnblogs.com/ForEvErNoME/
You are welcome to repost or share it, but be sure to declare the source of the article. If the article is helpful to you, I hope you can Recommendation Or Follow

     
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.