ASP. NET MVC3

Source: Internet
Author: User

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.

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.