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