原文地址:http://haacked.com/archive/2010/05/16/three-hidden-extensibility-gems-in-asp-net-4.aspx/

ASP.NET 4 introduces a few new extensibility APIs that live the hermit lifestyle away from the public eye. They’re not exactly hidden - they are well documented on MSDN - but they aren’t well publicized. It’s about time we shine a spotlight on them.

PreApplicationStartMethodAttribute

This new attribute allows you to have code run way early in the ASP.NET pipeline as an application starts up. I mean way early, even beforeApplication_Start.

This happens to also be before code in your App_code folder (assuming you have any code in there) has been compiled.

To use this attribute, create a class library and add this attribute as an assembly level attribute. A common place to add this would be in theAssemblyInfo.cs class within the Properties folder.

Here’s an example:

[assembly: PreApplicationStartMethod(
typeof(SomeClassLib.Initializer), "Initialize")]

Note that I specified a type and a method. That method needs to be a public static void method with no arguments. Now, any ASP.NET website that references this assembly will call the Initialize method when the application is about to start, giving this method a chance to do perform some early initialization.

public static class Initializer
{
public static void Initialize() {
// Whatever can we do here?
}
}

The primary use of this feature is to enable tasks that can’t be done withinApplication_Start because it’s too late. For example, registering build providers and adding assembly references.

Which leads us to…

BuildProvider.RegisterBuildProvider

As you might guess, if one of the key scenarios for the previously mentioned feature is to allow registering build providers, well ASP.NET better darn well allow you to register them programmatically.

Prior to ASP.NET 4, the only way to register a custom build provider was via the <buildproviders> node within web.config. But now, you can register them programmatically via a call to the new BuildProvider.RegisterBuildProvider method.

BuildProvider.RegisterBuildProvider(".foo", typeof(MyBuildProvider));

Combining the PreApplicationStartMethodAttribute with this method call means that installing a build provider can be done in one step -simply reference the assembly with the build provider and the assembly can register it for you. Whereas before, you would have to reference the assembly and then muck around with web.config.

I think I speak for us all when I say “Yay! Less junk in my web.config trunk!”

BuildManager.AddReferencedAssembly

Another new method added in ASP.NET 4 allows adding an assembly to the application’s list of referenced assemblies. This is equivalent to adding an assembly to the <assemblies> section of web.config.

As you might guess, this comes in handy when registering a custom build provider. It allows you to programmatically add references to assemblies that may be needed by your build provider.

Oh, and it’s yet another way to reduce the size of your web.config file. Who doesn’t love that? :)

最新文章

  1. css sprite 调整大张图片中小图标的大小
  2. 批量创建AD测试账号
  3. oracle基础学习
  4. C# exe dll防止反编译-- dotNET_Reactor
  5. ASP.NET MVC程序中动态修改form的Action值
  6. Cordova调用Activity
  7. Expression&lt;Func&lt;T,TResult&gt;&gt;和Func&lt;T,TResult&gt; 与AOP与WCF
  8. [iOS 多线程 &amp; 网络 - 2.8] - 检测网络状态
  9. 探讨一个新的两个进程间的通信和编程模型 (Windows)
  10. ios开发之UIView的frame、bounds跟center属性的区别(附图)
  11. javascript mapping
  12. python3的变量作用域规则和nonlocal关键字
  13. Git:git diff 命令详解
  14. dilated convolutions:扩张卷积
  15. 常用类(Date,Calendar,Math,枚举)
  16. 20165321实验一Java开发环境的熟悉-1
  17. Deploying docker registry v2
  18. python-工厂方法模式
  19. JS播放声音
  20. 双面女间谍第一至五季/全集Alias迅雷下载

热门文章

  1. javaweb笔记四
  2. 【Java】 大话数据结构(3) 线性表之静态链表
  3. 【Java】 子字符串的比较(substring的==与equal()使用)
  4. 全局查询文件linux
  5. JSR教程2——Spring MVC数据校验与国际化
  6. QT5:使用QFile类读写文本乱码
  7. Mybatis通过注解方式实现批量插入数据库
  8. 【Ray Tracing in One Weekend 超详解】 光线追踪1-9 景深
  9. Xamarin iOS教程之显示和编辑文本
  10. R基础学习(三)-- 简单练习(shiny+mysql+barplot)