asp.net-mvc ASP 包中的绝对 URL
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13682671/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Absolute URL in ASP bundle
提问by Bobby B
I use a jQuery library for Google Maps, and it depends on the Google scripts to be loaded first. I'd like to be able to include both in the bundle as such:
我为 Google Maps 使用了 jQuery 库,它取决于首先加载的 Google 脚本。我希望能够将两者都包含在捆绑包中:
bundles.Add(new ScriptBundle("myfoobundle").Include(
"http://maps.googleapis.com/maps/api/js?sensor=false&libraries=places",
"~/scripts/jquery.fooplugin-{version}.js"
));
This doesn't seem to work (throws an exception complaining about the first string). And one may say that this shouldn't work because that absolute URL is not meant to be minified/bundled.
这似乎不起作用(引发异常抱怨第一个字符串)。有人可能会说这不应该起作用,因为绝对 URL 并不意味着要缩小/捆绑。
But the current approach is a hassle, as I need to ensure that the dependencies are correct, and that happens in different places (half the problem in the bundling code, the other half in the view).
但是目前的方法很麻烦,因为我需要确保依赖关系是正确的,而且这发生在不同的地方(一半问题在捆绑代码中,另一半在视图中)。
Would be nice to have a 1-step solution as above. Do I have any options in this regard?
有一个如上所述的 1 步解决方案会很好。在这方面我有什么选择吗?
UPDATE:
更新:
To address the comments regarding using a CDN as a solution: if I specify bundles.UseCdn = trueit has no effect, and I still get the exception The URL 'http://maps.googleapis.com/maps/api/js?sensor=false&libraries=places' is not valid. Only application relative URLs (~/url) are allowed. Also I'm unsure what the implication of doing that is in the first place, because I already use CDN support for jQuery, etc., so unsure how that would conflictwith my use case.
解决关于使用 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 支持,所以不确定这与我的用例有何冲突。
采纳答案by Hao Kung
Currently you would have to include a local copy of the jquery that you are depending on inside of the bundle, or you would have to manage the script tags as you mention. We are aware of this kind of depedency management issue and it falls under the category of asset management which we are tracking with this work item on codeplex
目前,您必须在包内包含您所依赖的 jquery 的本地副本,否则您将不得不按照您提到的方式管理脚本标签。我们知道这种依赖管理问题,它属于资产管理类别,我们正在使用codeplex 上的这个工作项目进行跟踪
回答by Noah Heldman
If you are using a version of System.Web.Optimization>= 1.1.2, there is a new convenient way of overriding the url's for Stylesand Scripts. In the example below, I am grabbing a CdnBaseUrlfrom web.configto use as the base url for all scripts and stylesheets:
如果您使用的是System.Web.Optimization>= 1.1.2的版本,则有一种新的便捷方法可以覆盖 url 的 forStyles和Scripts。在下面的示例中,我将获取一个CdnBaseUrlfromweb.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..."
));
}
}
回答by Tommy
Based on the MVC tutorials, your syntax is incorrect for creating a bundle from a CDN. And as others have said, ensure that you have the bundles.UseCdn = true;property set. Using the example on the MVC site- your code should reflect the following:
根据 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&libraries=places";
bundles.Add(new ScriptBundle("myfoobundle", jqueryCdnPath).Include(
"~/Scripts/jquery-{version}.js"));
}
回答by Dhrumil Bhankhar
If it is just a matter of getting absolute url in bundle then you can go for this.
如果只是在 bundle 中获取绝对 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);
}
}
This will basically take the bahvior from Scripts.Render and then apply absolute urls to it. Then in the view you have to write
这将基本上从 Scripts.Render 中获取 bahvior,然后对其应用绝对 url。然后在视图中你必须写
@Url.RenderScript("~/bundles/jquery")
instead of
代替
@Scripts.Render("~/bundles/jquery")
Enjoy coding!!...
享受编码!...
回答by Lord of Scripts
I tried this as suggested and it didn't work:
我按照建议尝试了这个,但没有用:
string googleMapsApiCDN = "http://maps.google.com/maps/api/js?sensor=false&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
));
The mygmap3-about.js script was rendered but the gmap3.min.js and the CDN script from google where both excluded.
渲染了 mygmap3-about.js 脚本,但 gmap3.min.js 和来自谷歌的 CDN 脚本都排除了.
回答by Chamila Chulatunga
Have you tried enabling CDN support and seeing if that allows the absolute URL to work:
您是否尝试过启用 CDN 支持并查看是否允许绝对 URL 工作:
bundles.UseCdn = true;

