In ASP.net, there are many ways to implement pseudo static, we mainly introduce the implementation of this interface by IHttpModule to solve the problem today.
For the entire application, the IHttpModule interface is needed if we need to process the requested address when the request occurs. Commonly used to implement pseudo static technology. is to turn a get-access query string into a separate file. However, the value in the query string is actually accessed in the program. Such as:
Http://www.cnsaiko.com/news.aspx?id=1
Change to
Http://www. Cnsaiko.com/news_1.aspx
The advantage of this is to help SEO and prevent SQL injection and so on. Of course, file extensions can also vary with server support.
The IHttpModule interface has two methods for our implementation:
Dispose () Disposes of the resources (except memory) that are used by modules that implement IHttpModule.
Init () initializes the module and prepares it for processing requests.
The first step: if you want to use IHttpModule in your application, you need to configure the Web.config file.
In the Web.config file, we need to cast the httpmodules element, which is in the system.web element. As follows:
<add name= "HttpModule" type= "Webapplication1.httpmodule"/>
Note: The type must be the body path (including the namespace) of the types that implement the IHttpModule interface.
Step two: Realize IHttpModule
Creates a new HttpModule class that is the same as the type namespace path in the configuration file.
Use this class to implement the IHttpModule interface.
The code we started will be written in the Init method, where the HttpApplication object context includes all the methods, properties, and events of the process object. So in this object, we can start the processing of a request event.
You can write in the Init method:
Context. BeginRequest + = new EventHandler (context_beginrequest);
The above code registers a delegate method for the request start event.
When you register a delegate method, you can press the TAB key two consecutive times after you write + +, and automatically generate the Context_beginrequest method.
Step three: About Context_beginrequest () method
In the Context_beginrequest method, we can redirect the request according to the request. This turns the virtual pseudo static path into the Get-way query string that our program needs.
To change the request, you must get the current request, and in ASP.net, use the HttpContext type to encapsulate all the requested objects. So, we can get the HttpContext object from the HttpApplication object just now. Such as:
HttpApplication application = sender as HttpApplication;
HttpContext context = Application. context;