There are many pseudo-static methods in ASP. NET. We will introduce how to solve the problem through the IHttpModule interface today.
Compared with the entire application, if we need to process the request address when the request occurs, we need to use the IHttpModule interface. Pseudo-static technologies are commonly used. A Get query string is converted into an independent file. However, the value in the query string is actually accessed in the program. For example:
Http: // www.cnsaiko.com/news.aspx? Id = 1
Changed
Http: // www. cnsaiko.com/news_1.aspx
The benefit of doing so lies in the benefits of SEO and the prevention of SQL injection. Of course, the file extension can also be changed when the server supports it.
The IHttpModule interface has two methods for implementation:
Dispose () processes resources used by modules implementing the IHttpModule (excluding memory ).
The Init () initialization module prepares for processing requests.
Step 1: If you want to use IHttpModule in an application, you need to prepare the web. config file.
In the Web. config file, we need to set the httpModules element, which is in the system. web element. As follows:
1.
2. <add name = "HttpModule" type = "WebApplication1.HttpModule"/>
3.
Note: The type must be an all-in-one path (including a namespace) that implements the IHttpModule interface ).
Step 2: Implement IHttpModule
Create an HttpModule class, which is the same as the type namespace path in the preparation file.
Use this class to implement the IHttpModule interface.
The code we started will be written in the Init method. The HttpApplication object context in this method includes all the methods, attributes, and events of the program object. In this object, we can start processing a request event.
You can write in the Init method:
1. context. BeginRequest + = new EventHandler (context_BeginRequest );
The above code registers a delegate method for the request start event.
When registering a delegate method, you can press the Tab key twice after writing + =. The context_BeginRequest method is automatically generated.
Step 3: About the context_BeginRequest () method
In the context_BeginRequest method, we can redirect requests based on different requests. In this way, the virtual pseudo-static path can be changed to the Get query string required by our program.
To change the request, you must obtain the current request. In ASP. NET, use the HttpContext type to encapsulate all request objects. Then, we can get the HttpContext object through the HttpApplication object. For example:
1. HttpApplication application = sender as HttpApplication;
2. HttpContext context = application. Context;
Step 4: GET requests and redirection
(1) request:
You can use the property Request of the HttpContext object to obtain the current Request.
After obtaining the current request, you can use a regular expression to obtain the required variable information from the request and redirect it to a new location.
(2) redirection:
You can use the RewritePath method of the HttpContext object to put the new path in the method parameter.
After the principles are clear, let's look at the code implementation:
1. using System;
2. using System. Collections. Generic;
3. using System. Linq;
4. using System. Web;
5. using System. Text. RegularExpressions;
6.
7. // <summary>
8. // Summary of HttpModel
9. // </summary>
10. public class HttpModule: IHttpModule
11 .{
12. public void Dispose ()
13 .{
14. throw new NotImplementedException ();
15 .}
16.
17. public void Init (HttpApplication context)
18 .{
19. context. BeginRequest + = new EventHandler (context_BeginRequest); // Method for registering a BeginRequest event
20 .}
21.
22. void context_BeginRequest (object sender, EventArgs e)
23 .{
24. HttpApplication application = (HttpApplication) sender; // obtain the current application object
25. HttpContext context = application. Context; // obtain the context of the current request
26.
27. string url = context. Request. Url. ToString (); // obtain the false URL of the current Request.
28. int lastindex = url. LastIndexOf ('/'); // retrieve the index from the last one
29. string filename = url. Substring (lastindex); // get the file name
30. Regex reg = new Regex (@ "(\ d +) \. aspx"); // create a regular object to verify that the file name meets the conditions
31. if (reg. IsMatch (filename ))
32 .{
33. Match match = reg. Match (filename); // create a Match object
34. string id = match. Groups [1]. Value; // obtain the Value (ID) of the number in the group () through the regular result object)
35.
36. context. RewritePath ("View. aspx? Id = "+ id); // use the specified ID to overwrite the existing URL address.
37 .}
38.
39 .}
40 .}
This article is from the "Sharing Experience" blog
Source code download: http://www.bkjia.com/uploadfile/2011/1017/20111017055918255.zip