Accessing CDN bundles on different ASP.Net MVC site

筅森魡賤 提交于 2019-12-11 04:26:48

问题


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

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

  1. Main website (ASP.Net MVC website that has not JS or CSS resources)
  2. CDN website (ASP.Net MVC website that has all JS and CSS resources but not much else)

CDN Website Web.config extract

  <system.webServer>
    <httpProtocol>
      <customHeaders>
        <add name="Access-Control-Allow-Origin" value="*" />
      </customHeaders>
    </httpProtocol>
  </system.webServer>

CDN Website Bundle Config

public static class BundleConfig
{
    public static void RegisterBundles(BundleCollection bundles)
    {
        bundles.Add(new StyleBundle("~/Content/css").Include("~/Content/site.css"));
    }
}

Main Website Bundle Config

public static class BundleConfig
{
    public static void RegisterBundles(BundleCollection bundles)
    {
        bundles.Add(new StyleBundle("~/Content/css", "http://[CDN website]/Content/css"));
        //BundleTable.EnableOptimizations = true;
    }
}

Main Website Layout View

<!DOCTYPE html>
<html>
<head>
    @Styles.Render("~/Content/css")
</head>
<body>
    @RenderBody()
</body>
</html>

Result

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">

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

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

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.


回答1:


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

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

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.



来源:https://stackoverflow.com/questions/48179363/accessing-cdn-bundles-on-different-asp-net-mvc-site

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!