Module initialization implements pseudo-static address and module initialization pseudo-static

Source: Internet
Author: User

Module initialization implements pseudo-static address and module initialization pseudo-static

In the web. config configuration file <system. web> </system. web> write code (in version 2.0) (if there is a configuration file in version 3.5, you only need to replace the content in <add/>)

 

Create an HttpModule class file in the App_Code file, inherit the IHttpModule interface, and implement the interface

Public class HttpModule: IHttpModule {public void Dispose () {throw new NotImplementedException ();} public void Init (HttpApplication context) {context. beginRequest + = new EventHandler (context_BeginRequest); // The first event in asp.net's response to the request} // address 2: http://localhost:3087/weijingtai/1.aspx // Pseudo Static Page // address 1: http://localhost:3087/weijingtai/News.aspx?id=1 // The role of this event is: // change connection address 1. aspx to News. aspx? Id = 1 void context_BeginRequest (object sender, EventArgs e) {HttpApplication application = sender as HttpApplication; HttpContext context = application. context; // obtain the http information of the current request string filepath = context. request. rawUrl; // obtain the original url of the current request, int I = filepath. lastIndexOf ('/'); // obtain the index string fileName = filepath starting with the file name. substring (I); Regex rg = new Regex (@ "/(\ d + ). aspx "); // create a regular expression if (rg. isMatch (fileName) {Ma Tch match = rg. Match (fileName); string id = match. Groups [1]. Value; // get the file name context. RewritePath ("News. aspx? Id = "+ id); // spliced address }}}

Example:
<Li> <a href = "News. aspx? Id = 1 "> This is the first news </a> </li> connection 1
<Li> <a href = "1. aspx"> This is the first news </a> </li> connection 2
Connection 1 uses address 1. The request response goes through the context_BeginRequest event before the response. Because the regular expression does not match, the event does not take effect.
Then respond to the page
If (Request. QueryString ["id"]! = Null)
{
Response. Write ("This is the first" + Request. QueryString ["id"] + "news ");
}
Send back request page
Connection 2 uses address 2 to request a response. Before the response, the context_BeginRequest event matches the regular expression, so address 2 is changed to address 1.
Then respond to the page
If (Request. QueryString ["id"]! = Null)
{
Response. Write ("This is the first" + Request. QueryString ["id"] + "news ");
}
Send back request page

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.