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.

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.

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 (){}}

This function is far more than just the exception record here. For the writing framework, it can be said that it is of great use. You can understand it yourself.

Summary

The following is your favorite summary, which includes three parts:

1. I hope to keep an eye on my other articles.

2. Are there any clear instructions in the blog, or you have a better way? Join the two chat groups in the upper left corner to study and discuss them together.

3. You can forget to pay attention to likes, but never forget to scan the QR code to enjoy them.

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.