ASP.NET: PreApplicationStartMethod example
Few days back when I was exploring how ASP.NET 4.5 and MVC 4 bundling are implemented specially how bundle url are resolved at runtime. At that time I found that PreApplicationStartMethod is used to hookup bundle url. PreApplicationStartMethod is used to configure ASP.NET application at runtime. PreApplicationStartMethod is fired in early stage of application startup and hence it is executed before Application_Start event so we can’t get reference of HttpApplication or HttpContext from within PreApplicationStartMethod. To create Pre application start method, we need to create a class with static method and then after we have to hookup that method through PreApplicationStartMethod attribute.
public class PreApplicationStart
{
public static void Start()
{
// Pre application startup configuration goes here
}
}
In AssemblyInfo.cs, we need to add following line.
[assembly: PreApplicationStartMethod(typeof(PreApplicationStart), "Start")]
As we noted earlier we can’t refer HttpApplication or HttpContext here however we can access application related configuration through System.Web.Hosting.HostingEnvironment. Through hosting environment API, we can access current application settings as well Application Host settings for IIS.
PreApplicationStartMethod can be used for many purposes for e.g. registering HTTP module or HTTP handler at runtime and possibilities are unlimited. Yeah stay tuned for more exciting use of PreApplicationStartMethod.
You can get latest update via @NandipMakwana.