MVC-custom HttpModule processing, mvc-httpmodule

Source: Internet
Author: User

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:

 

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.