Asp.net mvc 4 在应用程序中访问HttpContext\u启动ASP.NET MVC 4.0

Asp.net mvc 4 在应用程序中访问HttpContext\u启动ASP.NET MVC 4.0,asp.net-mvc-4,httpcontext,application-start,Asp.net Mvc 4,Httpcontext,Application Start,我想访问我的主机并构建我的包。 我知道在应用程序_start中访问httpcontext是不直接的 您是否可以建议其他方法在RegisterBundles方法中查找Request.url.host Global.asax protected void Application_Start() { BundleConfig.RegisterBundles(BundleTable.Bundles); } public static string

我想访问我的主机并构建我的包。 我知道在应用程序_start中访问httpcontext是不直接的

您是否可以建议其他方法在
RegisterBundles
方法中查找
Request.url.host

Global.asax

 protected void Application_Start()
        {
          BundleConfig.RegisterBundles(BundleTable.Bundles);
        }
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"));
         }
BundleConfig.asax

 protected void Application_Start()
        {
          BundleConfig.RegisterBundles(BundleTable.Bundles);
        }
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"));
         }

编辑:建议替代方法

这里有几种方法可以做到这一点:

  • 创建捆绑包的两个版本
  • 添加一个局部视图,用于检查
    Request.IsLocal
    ,并呈现相应的捆绑包集
  • 从布局中调用局部视图
另一种方法是创建一个HtmlHelper方法,为您解析路径

另一种方法是创建多个布局,并确定要在viewstart中使用的正确布局


实现这一点的方法有很多,只有少数几种。

编辑:建议替代方法

这里有几种方法可以做到这一点:

  • 创建捆绑包的两个版本
  • 添加一个局部视图,用于检查
    Request.IsLocal
    ,并呈现相应的捆绑包集
  • 从布局中调用局部视图
另一种方法是创建一个HtmlHelper方法,为您解析路径

另一种方法是创建多个布局,并确定要在viewstart中使用的正确布局


实现这一点的方法有很多,只有少数。

由于我无法在应用程序启动事件中获取请求对象,因此我选择以下方法来完成我的任务。 我只想知道我的主机在应用程序启动事件

  • 创建Web.Config转换
  • 在每个环境中添加应用程序密钥,如
  • 将应用程序启动事件中的密钥读取为
    cdnHost=ConfigurationManager.AppSettings[“cdnReference”]

  • 由于我无法在应用程序启动事件中获取请求对象,因此我选择以下方法来完成任务。 我只想知道我的主机在应用程序启动事件

  • 创建Web.Config转换
  • 在每个环境中添加应用程序密钥,如
  • 将应用程序启动事件中的密钥读取为
    cdnHost=ConfigurationManager.AppSettings[“cdnReference”]

  • 我想您建议使用“LocalConstraint”类。但是,我如何调用这个“匹配”方法来获取“IsLocal”值对不起……我将稍微澄清一下我的回答。那么,你是说不要使用这个BundleConfig.RegisterBundles(BundleTable.Bundles);在应用程序启动时?不,继续并注册它们。仅仅因为您创建了一个bundle并不意味着您必须在您的站点上使用它。我猜您建议使用“LocalConstraint”类。但是,我如何调用这个“匹配”方法来获取“IsLocal”值对不起……我将稍微澄清一下我的回答。那么,你是说不要使用这个BundleConfig.RegisterBundles(BundleTable.Bundles);在应用程序启动时?不,继续并注册它们。仅仅因为你创建了一个包并不意味着你必须在你的网站上使用它。