سؤال

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"));
         }
هل كانت مفيدة؟

المحلول 2

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"];

نصائح أخرى

Edit: Suggesting alternate approaches.

Here's a couple ways to do this:

  • Create two versions of your bundles
  • Add a partial view that checks Request.IsLocal and renders the appropriate set of bundles
  • Call the partial view from your layout

An alternate approach would be to create an HtmlHelper method that resolves the path for you.

Yet another approach is to create multiple layouts, and determine the correct one to use in your viewstart.

There are many ways to implement this, there are just a few.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top