Accessing CDN bundles on different ASP.Net MVC sit

2019-07-31 02:34发布

问题:

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.