访问不同 ASP.Net MVC 站点上的 CDN 包
Accessing CDN bundles on different ASP.Net MVC site
这是一个后续问题:
我想从第二个 ASP.Net 网站提供我的 JavaScript 和 CSS 捆绑包,所以我做了以下操作。
- 主网站(ASP.Net没有JS或CSS资源的MVC网站)
- CDN 网站(ASP.Net MVC 网站,拥有所有 JS 和 CSS 资源,但其他资源不多)
CDN网站Web.config提取
<system.webServer>
<httpProtocol>
<customHeaders>
<add name="Access-Control-Allow-Origin" value="*" />
</customHeaders>
</httpProtocol>
</system.webServer>
CDN 网站捆绑配置
public static class BundleConfig
{
public static void RegisterBundles(BundleCollection bundles)
{
bundles.Add(new StyleBundle("~/Content/css").Include("~/Content/site.css"));
}
}
主要网站捆绑配置
public static class BundleConfig
{
public static void RegisterBundles(BundleCollection bundles)
{
bundles.Add(new StyleBundle("~/Content/css", "http://[CDN website]/Content/css"));
//BundleTable.EnableOptimizations = true;
}
}
主要网站布局视图
<!DOCTYPE html>
<html>
<head>
@Styles.Render("~/Content/css")
</head>
<body>
@RenderBody()
</body>
</html>
结果
主网站生成的 HTML 不呈现 CSS 的 <link>
标签。但是,如果我打开捆绑优化(请参阅主网站捆绑配置中注释掉的代码),则会出现 <link>
标签,但看起来像这样:
<link href="/Content/css?v=" rel="stylesheet">
在浏览器中导航到 http://[CDN 网站]/Content/css 加载适当的 CSS.
导航到 http://[Main 网站]/Content/css 在浏览器中加载一个空页面。
我做错了吗?我不想直接引用 CDN 网站 URL,因为我想要 MVC 捆绑程序附带的版本控制。
我最终为 CDN 网站使用了以下解决方案。
然后,主网站使用 CDN 网站的适当 MVC 路由作为样式 "href" 和脚本 "src",重定向到适当版本的内容或脚本包。
这是一个后续问题:
我想从第二个 ASP.Net 网站提供我的 JavaScript 和 CSS 捆绑包,所以我做了以下操作。
- 主网站(ASP.Net没有JS或CSS资源的MVC网站)
- CDN 网站(ASP.Net MVC 网站,拥有所有 JS 和 CSS 资源,但其他资源不多)
CDN网站Web.config提取
<system.webServer>
<httpProtocol>
<customHeaders>
<add name="Access-Control-Allow-Origin" value="*" />
</customHeaders>
</httpProtocol>
</system.webServer>
CDN 网站捆绑配置
public static class BundleConfig
{
public static void RegisterBundles(BundleCollection bundles)
{
bundles.Add(new StyleBundle("~/Content/css").Include("~/Content/site.css"));
}
}
主要网站捆绑配置
public static class BundleConfig
{
public static void RegisterBundles(BundleCollection bundles)
{
bundles.Add(new StyleBundle("~/Content/css", "http://[CDN website]/Content/css"));
//BundleTable.EnableOptimizations = true;
}
}
主要网站布局视图
<!DOCTYPE html>
<html>
<head>
@Styles.Render("~/Content/css")
</head>
<body>
@RenderBody()
</body>
</html>
结果
主网站生成的 HTML 不呈现 CSS 的 <link>
标签。但是,如果我打开捆绑优化(请参阅主网站捆绑配置中注释掉的代码),则会出现 <link>
标签,但看起来像这样:
<link href="/Content/css?v=" rel="stylesheet">
在浏览器中导航到 http://[CDN 网站]/Content/css 加载适当的 CSS.
导航到 http://[Main 网站]/Content/css 在浏览器中加载一个空页面。
我做错了吗?我不想直接引用 CDN 网站 URL,因为我想要 MVC 捆绑程序附带的版本控制。
我最终为 CDN 网站使用了以下解决方案。
然后,主网站使用 CDN 网站的适当 MVC 路由作为样式 "href" 和脚本 "src",重定向到适当版本的内容或脚本包。