MVC-custom HttpModule processing, mvc-httpmodule
HttpModule provides module initialization and event handling for implementation classes.
When an HTTP request arrives at HttpModule, the entire ASP. the. NET Framework system does not process this HTTP request. That is to say, HttpModule is a "necessary path" for HTTP requests ", therefore, some required information can be appended to the HTTP request information before the HTTP request is passed to the real request processing center (HttpHandler, you can also perform some additional work on the intercepted HTTP request information, or simply terminate the HTTP request that meets certain conditions in some cases, so as to act as a Filter.
Next we will use the custom HttpModule module to perform some additional work on the HTTP request information intercepted by the user when making the request.
1. Create a class library project and add our entity class. HttpModule inherits the IHttpModule interface to implement the Dispose method and the Init event registration method.
Using System; using System. collections. generic; using System. linq; using System. text; using System. web; namespace MyHttpModule {public class HttpModule: IHttpModule {// <summary> // the disposal is implemented by System. web. resources used by the IHttpModule module (excluding memory) /// </summary> public void Dispose () {}/// <summary> // initialize the module, and make it ready to process the request. /// </Summary> /// <param name = "context"> </param> public void Init (HttpApplication context) {context. beginRequest + = context_BeginRequest; // in ASP. NET responds to the request as the first event in the HTTP execution pipeline. Context. EndRequest + = context_EndRequest; // when ASP. NET responds to a request, it occurs as the last event in the HTTP execution pipeline chain.} /// <Summary> /// when ASP. NET responds to a request, it occurs as the last event in the HTTP execution pipeline. /// </Summary> /// <param name = "sender"> </param> /// <param name = "e"> </param> void context_EndRequest (object sender, eventArgs e) {HttpApplication application = sender as HttpApplication; HttpContext context = application. context; HttpRequest request = application. request; HttpResponse response = application. response; response. write ("context_EndRequest> in ASP. NET responds to the request as the last event in the HTTP execution Pipeline ");} /// <Summary> /// when ASP. NET responds to a request, it is the first event in the HTTP execution pipeline chain. /// </Summary> /// <param name = "sender"> </param> /// <param name = "e"> </param> void context_BeginRequest (object sender, eventArgs e) {HttpApplication application = sender as HttpApplication; HttpContext context = application. context; HttpRequest request = application. request; HttpResponse response = application. response; response. write ("context_BeginRequest> in ASP. NET responds to the request as the first event in the HTTP execution pipeline chain ");}}}View Code
2. Add an MVC project with two controllers and view pages added.
Public class HomeController: Controller {// GET:/Home/public ActionResult Index () {return View ();}}View Code public class SonController: Controller {// GET:/Son/public ActionResult Index () {return View ();}}View Code @ {Layout = null ;}<! DOCTYPE html>
The IIS is published successfully. Add the configuration item under <system. Web> in web. config and put the MyHttpModule. dll library file to the bin directory:
<HttpModules> <! -- <Add name = "Class name" type = "namespace. Class name"/> <add name = "HttpModule" type = "MyHttpModule. HttpModule"/>
The requested URL. The page effect is as follows: