Detailed description of Mvc Dynamic Registration HttpModule and mvchttpmodule

Source: Internet
Author: User

Detailed description of Mvc Dynamic Registration HttpModule and mvchttpmodule

Preface

Registering Httpmodule allows us to process pipeline events in the HttpApplication object. Currently, we are familiar with using two methods to process pipeline events in the HttpApplication object. The first is to use the Global. asax Global file, and the other is to register the httpmodule through the configuration file. So there are two ways. Why do we need to use this blog today?

Here I also raised a simple question, using instances to prove the merits of Dynamic Registration of httpmodule.

If you want to write. net Framework for all mvc projects of the company. Therefore, at least one exception record function should be required for all the functions integrated by your framework. With this function, even if someone else's project forgets to handle the exception information, the framework will also help you write the project exception information into the framework log without pulling it. How can this problem be solved? If someone else doesn't need to write a line of code to implement this function, how can this function be implemented? In your framework, you only need a few lines of code.

Microsoft. Web. Infrastructure. dll and PreApplicationStartMethodAttribute attributes

First, let's use ilspy to see what the Microsoft. Web. Infrastructure. dll class library does,

 

[SecuritySafeCritical]public static void RegisterModule(Type moduleType){  if (DynamicModuleReflectionUtil.Fx45RegisterModuleDelegate != null)  {DynamicModuleReflectionUtil.Fx45RegisterModuleDelegate(moduleType);    return;  }DynamicModuleUtility.LegacyModuleRegistrar.RegisterModule(moduleType);}

After you see the source code of such a library, you can easily find out what it is.

  1. Microsoft. Web. Infrastructure. DynamicModuleHelper this namespace we can see that it is about the dynamic module helper.
  2. DynamicModuleUtility is a classification class. When it is opened, you can see a striking method: RegisterModule, register Module. That's right, it is the method for dynamically registering HttoModule.

Think about my proposition above. We can execute our class library without having to execute a line of code in the project, so we can register the HttpModule dynamically. Next, we will not write a line of code, can we run our class library? This applies to the PreApplicationStartMethodAttribute attribute provided by Microsoft.

  1. Preapplicationstartmethodattri: This attribute can also be defined as a method to run before the application starts. That is to say, he is also the entry point of a program and started before application initialization. It is the first thing that comes with everything. Let's create it.
  2. WebActivatorEx: This is a class library. Microsoft provides the PreApplicationStartMethodAttribute feature. A member of the Microsoft Nuget Development Team, David Ebbo, writes this even more embarrassing class library. The content is shown in figure

This class library can dynamically register HttpMoudle. It can also add the Application_Start ()/Shutdown () event without modifying the global file. You can view the source code using ilspy, you can also obtain the git address of the project through nuget, which is worth your research.

Code implementation: dynamically register HttpMoudle

[assembly: WebActivatorEx.PreApplicationStartMethod(typeof(RoutingCore.PreApplicationStartRegist), "PreStart")]namespace RoutingCore{  public class PreApplicationStartRegist  {    private static bool hasLoaded;    public static void PreStart()    {      if (!hasLoaded)      {        hasLoaded = true;        DynamicModuleUtility.RegisterModule(typeof(RoutingModule));      }    }  }}
Public class RoutingModule: IHttpModule {public void Init (HttpApplication context) {context. beginRequest + = new EventHandler (context_BeginRequest); context. error + = new EventHandler (context_ErrorRequest);} void context_BeginRequest (object sender, EventArgs e) {} void context_ErrorRequest (object sender, EventArgs e) {HttpApplication ap = sender as HttpApplication; var error = ap. server. getLast Error (); var code = (error is HttpException )? (Error as HttpException). GetHttpCode (): 500; if (code! = 404) {}// write a local file, queue or database //... ap. server. clearError (); ap. response. write (error. message); ap. response. end () ;}public void Dispose (){}}

The above is all the content of this article. I hope this article will help you in your study or work. I also hope to provide more support to the customer's home!

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.