简体   繁体   中英

Change Global.asax via NuGet package?

I am creating a NuGet package and was wondering if there's a way to modify the Global.asax of the target website? I would like to add one line in the Application_Start (and create the Global.asax if it is not there). Is this possible? How would updates work?

The recommended approach is not to modify the Global.asax file of the host application. Instead you could use WebActivator and add a separate file to the project. Take a look for example at the Ninject.MVC3 NuGet which does exactly that.

For example when yo install your NuGet you could simply add the following file to the project ~/App_Start/MyNuGetAppStart.cs :

[assembly: WebActivator.PreApplicationStartMethod(typeof(SomeNamespace.AppStart), "Start")]

namespace SomeNamespace
{

    public static class AppStart
    {
        /// <summary>
        /// Will run when the application is starting (same as Application_Start)
        /// </summary>
        public static void Start() 
        {
            ... put your initialization code here
        }
    }
}

This is a far more unobtrusive way to add custom code at application startup rather than messing with the Global.asax file which the user might have already tweaked.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM