asp.net MVC3 about how to create a pure static after how to stop walking by direct Access static page _ Practical Tips

Source: Internet
Author: User
Tags httpcontext

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.

Related Article

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.