ASP包中的绝对URL

Bob*_*y B 19 asp.net-mvc asp.net-mvc-4 bundling-and-minification asp.net-optimization

我为谷歌地图使用了一个jQuery库,它取决于首先加载的谷歌脚本.我希望能够在包中包含这两个:

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

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

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

如上所述,拥有一步解决方案会很高兴.我在这方面有什么选择吗?

更新:

解决有关使用CDN作为解决方案的意见:如果我指定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支持,所以不确定这会如何与我的用例冲突.

Noa*_*man 9

如果您正在使用的版本System.Web.Optimization> = 1.1.2,有重写的URL为一个新的便捷的方式StylesScripts.在下面的示例中,我抓取一个CdnBaseUrlfrom web.config作为所有脚本和样式表的基本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..."
        ));
    }
}
Run Code Online (Sandbox Code Playgroud)

有关静态站点(CDN)优化的更多信息


Hao*_*ung 7

目前,您必须在捆绑包内包含您依赖的jquery的本地副本,或者您必须在提及时管理脚本标记.我们知道这种依赖管理问题,它属于资产管理的范畴,我们正在跟踪这个关于Codeplex的工作项目


Tom*_*mmy 6

基于MVC教程,您的语法不正确,无法从CDN创建捆绑包.正如其他人所说,确保你拥有bundles.UseCdn = true;财产.使用MVC站点上的示例- 您的代码应反映以下内容:

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"));
}
Run Code Online (Sandbox Code Playgroud)

  • 这段代码允许我从CDN加载jQuery,我已经这样做了.我想要的是将一些其他不相关的库(谷歌地图的东西)作为绝对URL包含在相应的包中. (6认同)

Dhr*_*har 5

如果只是在捆绑中获取绝对URL,那么你可以去做.

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);
        }
    }
Run Code Online (Sandbox Code Playgroud)

这将基本上从Scripts.Render采取bahvior,然后应用绝对URL.然后在视图中你必须写

  @Url.RenderScript("~/bundles/jquery")
Run Code Online (Sandbox Code Playgroud)

代替

  @Scripts.Render("~/bundles/jquery")
Run Code Online (Sandbox Code Playgroud)

享受编码!! ...