To solve this problem, we need to first understand the lifecycle of ASP. NET applications, first look at an image prepared by the author below:
We can clearly see that when a common IIS accesses an application, each time a single page URL is accessedHttpApplication pipeline processes requests,After the BeginRequest event is passed, the system will walk and access the specific Controller and Action. At the end, the system will request the EndRequest event. The following figure shows the sequence:
Note that the red part marked in the figure is what we want to implement. The implementation is as follows:
1. Create MyHandler. cs
Copy codeThe Code is as follows: public class MyHandler: IHttpModule
{
Public void Init (HttpApplication application)
{
Application. BeginRequest + =
(New EventHandler (this. Application_BeginRequest ));
Application. EndRequest + =
(New EventHandler (this. Application_EndRequest ));
}
Private void Application_BeginRequest (Object source,
EventArgs e)
{
// Create HttpApplication and HttpContext objects to access
// Request and response properties.
HttpApplication application = (HttpApplication) source;
HttpContext context = application. Context;
String filePath = context. Request. FilePath;
String fileExtension =
VirtualPathUtility. GetExtension (filePath );
If (fileExtension. Equals (". html "))
{
Context. Response. WriteFile (context. Server. MapPath (filePath); // directly go to the static page
// Cache can be added here, and the conditions can be defined as needed
Context. Response. End ();
}
}
Private void Application_EndRequest (Object source, EventArgs e)
{
HttpApplication application = (HttpApplication) source;
HttpContext context = application. Context;
String filePath = context. Request. FilePath;
String fileExtension =
VirtualPathUtility. GetExtension (filePath );
If (fileExtension. Equals (". html "))
{
Context. Response. Write (""HelloWorldModule: End of Request </font> }
}
Public void Dispose (){}
}
2. Add the following code to web. config to run the custom MPs class.
Copy codeThe Code is as follows: <Add name = "MvcTest. MyHandler" type = "MvcTest. MyHandler"/>
</HttpModules>
Run your own code to see the effect!
Supplement: As prompted by @ xiaoxuyu, if the same directory file as the URL is directly produced under your project file, for example, access: TaobaoCopy codeThe Code is as follows: private void Application_BeginRequest (Object source,
EventArgs e)
{
// Create HttpApplication and HttpContext objects to access
// Request and response properties.
HttpApplication application = (HttpApplication) source;
HttpContext context = application. Context;
String filePath = context. Request. FilePath;
String fileExtension =
VirtualPathUtility. GetExtension (filePath );
If (fileExtension. Equals (". html "))
{
// Determine whether the cache exists and does not exist. Call to generate static classes and Methods
// Product expired, remove static files, 302 redirection
If (System. IO. File. Exists (context. Server. MapPath (filePath )))
{
Context. Response. WriteFile (context. Server. MapPath (filePath ));
Context. Response. End ();
}
}
This is a general idea.