ASP. MVC3 about how to generate pure static and how to stop walking by direct access to static pages--collection not measured

Source: Internet
Author: User

To solve this problem, we need to understand the life cycle of the ASP, first look at an image that the following authors have organized:

We can clearly see: When the general IIS accesses the application, each time a single page URL is accessed, it goes through the HttpApplication pipeline processing request, walks through the BeginRequest event before walking by accessing the specific controller and action , the EndRequest event is requested at the end. This sequence is represented by a graph:

Note that the red part of the figure is the part we want to implement, as follows:
1 new MyHandler.cs


Copy the code code 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));//Go straight to the static page
The cache can be added here, and the condition 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 pipeline processing class
Copy the code code as follows:
<add name= "Mvctest.myhandler" type= "Mvctest.myhandler"/>

Run your own code and see how it works!
Add: According to the tip of the @ small tail fish, if directly under their own project file production and URL in the same directory file, such as access: yourdomin.com/product/1.html, your project folder really exists product/1.html this path, Then IIS will go directly to request this static page, if the project uses a custom pipeline handler, then this static page will still walk our custom pipeline handler, we can be here through the cache to do not want to re-grow static pages or delete outdated products static page, if you do not use this method, Can only write execution plan, run these static files regularly, modify Application_BeginRequest
Copy the 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"))
{
Determines whether the cache exists, does not exist to join the cache, calls to generate static classes and methods
Product expires, removal of static files, 302 redirects
if (System.IO.File.Exists (context. Server.MapPath (FilePath)))
{
Context. Response.WriteFile (context. Server.MapPath (FilePath));
Context. Response.End ();
}
}

This is the general idea.
For more information, refer to: http://www.jb51.net/article/29199.htm

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.