asp.net 4.0 some recessive extensions

Source: Internet
Author: User
Keywords We extend some this function
Tags .net asp asp.net blog code example framework function

ASP.net 4.0 has been improved in many ways, and the ASP.net 4.0 white paper describes a number of ASP.net 4.0 mechanism changes and improvements. In my blog, there are also several articles about asp.net4.0 's feature modification. But as a whole new framework and runtime, there is certainly a lot of internal APIs and extension points that are not exposed as clearly. Today, for example, from this article Three Hidden extensibility Gems in asp.net 4, I learned about some of the extension points that are absolutely useful in my usual development.

Preapplicationstartmethodattribute

This new attribute allows us to specify a public static function that executes the function before the Application_Start of the site. If your site has an App_Code directory, this function will also execute before the code in the App_Code directory is compiled. From my intuition, this is a fairly useful extension point.

We must use this attribute from the assembly level, which is usually used in AssemblyInfo.cs:

[Assembly:preapplicationstartmethod (
typeof (Someclasslib.initializer), "Initialize")]

What we need to specify is the name of the static function within the type and type, which must be a public function with no parameters and return values.

The most important thing about this feature is that it can do things that we didn't do in Application_Start, so a lot of things have been done to Application_Start, not to be changed, like the following extensions for compiling.

Buildprovider.registerbuildprovider

Originally we want to register buildprovider by adding Web.config <buildproviders> to complete. In ASP.net 4.0, we can cooperate with Preapplicationstartmethodattribute, add custom to BuildProvider to achieve the goal before the site starts.

buildmanager.addreferencedassembly

doing. aspx/. When compiling code files in the ASPCX and App_Code directories, you need to rely on some assemblies. Previously, we had to configure these assemblies to be completed in the Web.config <assemblies> node. Now you just need to work with the above attribute and the new method to increase these dependencies directly in the form of code.

Config-free IHttpModule Registration

This is also an absolute magical effect on preapplicationstartmethodattribute, in the Nikhil Kothari This article has detailed introduction. Its main goal, also is to break away from Web.config can register IHttpModule by code.

In short, although it is a simple attribute, but it is very useful. When we develop a reusable framework, we inevitably need to do a lot of relevant configuration in the program to get the program running. In the past, we had to ask the user to do it through web.config, and when the user had one less configuration, the entire framework could not run. If we can do all of these necessary configurations within our framework, this will greatly reduce the threshold of use of the framework.

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.