Search code examples
asp.net-mvc-4httpcontextapplication-start

Access HttpContext in Application_Start ASP.NET MVC 4.0


I would like to access my host and build my bundles. I know it is not straight to access httpcontext in Application_start.

Can you advise any other work around to find the Request.url.host in RegisterBundles Method

Global.asax

 protected void Application_Start()
        {
          BundleConfig.RegisterBundles(BundleTable.Bundles);
        }

BundleConfig.asax

public static string host { get; set; }
        public static string siteCssFileName { get; set;  }
        public static void RegisterBundles(BundleCollection bundles)
        {
host = "~";
            if (System.Web.HttpContext.Current.Request.Url.Host.Contains("local"))
                host = "http://localhost:xxx";
                else
                host=Request.url.host;
                bundles.Add(new StyleBundle("~/Content/jQuery/ui/css")
                  .Include(host+"/css/jQuery/hro-0079c1/jquery-ui-1.8.11.custom.css"));
         }

Solution

  • Since I could not get my Request object in application start event, I have choose the below approach to achieve my task. All that I wanted to know was my host in app start event.

    1. Create Web.Config Transform
    2. Add a app key in each environment like <add key="cdnReference" value="http://localhost:xxxx/" />
    3. read the key in your app start event as cdnHost = ConfigurationManager.AppSettings["cdnReference"];