【问题标题】:Absolute URL in ASP bundleASP 包中的绝对 URL
【发布时间】:2012-12-03 11:51:15
【问题描述】:

我为谷歌地图使用了一个 jQuery 库,它依赖于首先加载的谷歌脚本。我希望能够将两者都包含在捆绑包中:

  bundles.Add(new ScriptBundle("myfoobundle").Include(
    "http://maps.googleapis.com/maps/api/js?sensor=false&libraries=places",
    "~/scripts/jquery.fooplugin-{version}.js"
  ));

这似乎不起作用(引发抱怨第一个字符串的异常)。有人可能会说这不应该起作用,因为绝对 URL 并不意味着要缩小/捆绑。

但是当前的方法很麻烦,因为我需要确保依赖关系是正确的,而且这发生在不同的地方(一半在捆绑代码中,另一半在视图中)。

如果有上述的 1 步解决方案,那就太好了。在这方面我有什么选择吗?

更新:

解决有关使用 CDN 作为解决方案的 cmets:如果我指定 bundles.UseCdn = true 它没有效果,我仍然得到异常 The URL 'http://maps.googleapis.com/maps/api/js?sensor=false&libraries=places' is not valid. Only application relative URLs (~/url) are allowed。另外我不确定这样做的意义是什么,因为我已经使用了对 jQuery 等的 CDN 支持,所以不确定这会如何与我的用例冲突

【问题讨论】:

  • 第一个字符串的异常说明了什么?如果您将UseCdn 设置为true,您的代码应该可以工作。
  • @rob 它不适用于启用UseCdn
  • asp 包是蓝表
  • 我注意到的行为是 bundles.UseCdn = true 并且您在 system.web 下的编译方法必须为 false ( )。然后将CDN路径输出到页面。

标签: asp.net-mvc asp.net-mvc-4 asp.net-optimization bundling-and-minification


【解决方案1】:

如果您使用的是System.Web.Optimization >= 1.1.2 版本,则有一种新的便捷方法可以覆盖StylesScripts 的网址。在下面的示例中,我从 web.config 中获取了一个 CdnBaseUrl,用作所有脚本和样式表的基本 url:

public class BundleConfig
{
    private static readonly string BaseUrl = ConfigurationManager.AppSettings["CdnBaseUrl"];

    public static void RegisterBundles(BundleCollection bundles)
    {
        // This is the new hotness!!
        Styles.DefaultTagFormat = "<link href=\"" + BaseUrl + "{0}\" rel=\"stylesheet\"/>";
        Scripts.DefaultTagFormat = "<script src=\"" + BaseUrl + "{0}\"></script>";

        bundles.Add(new ScriptBundle("~/bundles/js").Include(
            "Your scripts here..."
        ));

        bundles.Add(new StyleBundle("~/bundles/css").Include(
            "Your css files here..."
        ));
    }
}

More info on static site (CDN) optimization

【讨论】:

    【解决方案2】:

    目前,您必须在捆绑包中包含您所依赖的 jquery 的本地副本,或者您必须管理您提到的脚本标签。我们知道这种依赖管理问题,它属于我们正在跟踪的资产管理类别work item on codeplex

    【讨论】:

    • 感谢浩的明确回答!我希望你们尽快解决这个问题。我一直在寻找一种将远程脚本包含到包中的方法(不是像该 codeplex 工作项中解释的那样复杂。
    【解决方案3】:

    根据 MVC 教程,您的语法对于从 CDN 创建捆绑包是不正确的。正如其他人所说,确保您设置了 bundles.UseCdn = true; 属性。使用MVC site 上的示例 - 您的代码应反映以下内容:

    public static void RegisterBundles(BundleCollection bundles)
    {
       bundles.UseCdn = true;   //enable CDN support
       //add link to jquery on the CDN
       var jqueryCdnPath = "http://maps.googleapis.com/maps/api/js?sensor=false&amp;libraries=places";
       bundles.Add(new ScriptBundle("myfoobundle", jqueryCdnPath).Include(
                    "~/Scripts/jquery-{version}.js"));
    }
    

    【讨论】:

    • 这段代码将允许我从 CDN 加载 jQuery,我已经这样做了。我想要的是在适当的包中包含一些其他不相关的库(谷歌地图的东西)作为绝对 URL。
    【解决方案4】:

    如果只是在捆绑包中获取绝对网址,那么您可以这样做。

    public static class Extensions
        {
            public static IHtmlString RenderScript(this UrlHelper helper, params string[] paths)
            {
                string scripts = System.Web.Optimization.Scripts.Render(paths).ToHtmlString();
                string hostName = HttpContext.Current.Request.Url.Scheme + Uri.SchemeDelimiter + HttpContext.Current.Request.Url.Authority;
                string replaced = Regex.Replace(scripts, "src=\"/", "src=\"" + hostName + "/", RegexOptions.Multiline | RegexOptions.IgnoreCase);
                return new HtmlString(replaced);
            }
        }
    

    这基本上会从 Scripts.Render 中获取 bahvior,然后对其应用绝对 url。然后在视图中你必须写

      @Url.RenderScript("~/bundles/jquery")
    

    而不是

      @Scripts.Render("~/bundles/jquery")
    

    享受编码!...

    【讨论】:

    • 谢谢德鲁米尔!当我尝试将异步标记与捆绑的 jquery 文件一起使用时,这对我有所帮助。
    【解决方案5】:

    我按照建议尝试了,但没有成功:

    string googleMapsApiCDN = "http://maps.google.com/maps/api/js?sensor=false&amp;language=en";
            bundles.Add(new ScriptBundle("~/bundles/gmap3", googleMapsApiCDN).Include(
                        "~/Scripts/GMap3/gmap3.min.js",         // GMap3 library
                        "~/Scripts/GMap3/mygmap3-about.js"      // Pops up and configures     
    GMap3 on About page
                        ));
    

    mygmap3-about.js 脚本已渲染,但 gmap3.min.js 和来自 google 的 CDN 脚本都排除在外

    【讨论】:

      【解决方案6】:

      您是否尝试过启用 CDN 支持并查看是否允许绝对 URL 工作:

      bundles.UseCdn = true;
      

      【讨论】:

      • 不确定它是否能回答问题,但很高兴知道有这样的选项。
      猜你喜欢
      • 2010-10-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-01-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-01-14
      相关资源
      最近更新 更多