Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 870
TruMan1 Avatar asked Mar 01 '13 23:03

TruMan1


1 Answers

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.

like image 190
Darin Dimitrov Avatar answered Sep 23 '22 08:09

Darin Dimitrov