本文介绍了访问不同ASP.Net MVC站点上的CDN捆绑包的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是对此的后续问题:

This is a follow-up question to this: ASP.Net MVC 5 - Deploy separate CDN site with bundled JavaScipt and CSS

我想从第二个ASP.Net网站提供我的JavaScript和CSS包,所以我做了以下工作.

I want to serve up my JavaScript and CSS bundles from a second ASP.Net website, so I have done the following.

  1. 主要网站(没有JS或CSS资源的ASP.Net MVC网站)
  2. CDN网站(具有所有JS和CSS资源但没有太多其他资源的ASP.Net MVC网站)

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>标记,但看起来像这样:

The HTML produced by the main website doesn't render the <link> tag for the CSS. However, if I turn on bundle optimizations (see commented out code in Main Website Bundle Config), then the <link> tag appears but looks like this:

<link href="/Content/css?v=" rel="stylesheet">

在浏览器中导航到 http://[CDN 网站]/Content/css 加载适当的CSS.

Navigating to http://[CDN website]/Content/css in the browser loads the appropriate CSS.

在浏览器中导航到 http://[Main 网站]/Content/css 加载一个空白页.

Navigating to http://[Main website]/Content/css in the browser loads an empty page.

我做错了吗?我不想直接引用CDN网站URL,因为我想要MVC捆绑程序随附的版本控制.

Am I doing this the incorrectly? I don't want to reference the CDN website URL directly because I want the versioning that comes with the MVC bundler.

推荐答案

我最终对CDN网站使用了以下解决方案.

I ended-up using the following solution for the CDN website.

https://stackoverflow.com/a/26402383/2663033

然后,主网站将CDN网站的相应MVC路由用于样式"href"和脚本"src",这些重定向到适当版本的内容或脚本包.

The main website then used the appropriate MVC route of the CDN website for the style "href" and the script "src", which redirected to the appropriately versioned content or script bundle.

这篇关于访问不同ASP.Net MVC站点上的CDN捆绑包的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-05 16:14