To solve this problem, we need to understand the lifecycle of the ASP.net application first, and look at one of the pictures that the author has sorted out:
From the diagram we can see clearly: when Universal IIS accesses an application, each time a single page URL is accessed, it goes through the HttpApplication pipeline processing request, passing through the beginrequest After the event will go to walk by accessing the specific controller and action, the end of the time will request the EndRequest event. Here's a graph to show this order:
Note that the red part of the diagram is the part we want to implement as follows:
1 new MyHandler.cs
Copy Code code as follows:
public class Myhandler:ihttpmodule
{
public void Init (HttpApplication application)
{
Application. BeginRequest =
(this.) New EventHandler (this. Application_BeginRequest));
Application. endrequest =
(this.) 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));/go straight to static page
Caching can be added here, and 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. The following code is added to the web.config to run the custom pipe processing class
Copy Code code as follows:
<add name= "Mvctest.myhandler" type= "Mvctest.myhandler"/>
Run your own code, see the effect you are all clear!
Add: According to the @ Small tail fish tips, if directly under their own project files produced and URL in the same directory file, such as access: yourdomin.com/product/1.html, your project folder really exist product/1.html this path, Then IIS will directly request this static page, if a custom pipe handler is used in the project, the static page will still go through our custom pipe handler, where we can implement the cache to either restart the static page or delete the static page of the expired product, if this method is not used, Can only write execution plan, run these static files regularly, modify Application_BeginRequest
Copy Code code 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"))
{
To determine whether the cache exists, there is no added cache, and calls to generate static classes and methods
Product expiration, remove static file, 302 redirect
if (System.IO.File.Exists context. Server.MapPath (FilePath))
{
Context. Response.WriteFile (context. Server.MapPath (FilePath));
Context. Response.End ();
}
}
The idea is generally the same.