在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
原文地址: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. PreApplicationStartMethodAttributeThis 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 before 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:
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
The primary use of this feature is to enable tasks that can’t be done within Which leads us to… BuildProvider.RegisterBuildProviderAs 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
Combining the I think I speak for us all when I say “Yay! Less junk in my web.config trunk!” BuildManager.AddReferencedAssemblyAnother 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 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? :)
|
请发表评论