prePEND CDN网址为MVC 4打捆输出 [英] Prepend CDN url to mvc 4 bundler output

查看:175
本文介绍了prePEND CDN网址为MVC 4打捆输出的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用内置的MVC4打捆,我怎么prePEND我CDN网址到它产生的链接标签?我设置亚马逊的Cloudfront,这样它从我的web服务器的资产第一次请求的时候。所以,当我定义一个包,像这样:

  bundles.Add(新StyleBundle(〜/内容/ CSS)。包括(
    〜/内容/ reset.css
    〜/内容/ 960_24_col.css
    〜/内容/的site.css
 ));

在部署时,我可以这样引用它:

<$p$p><$c$c>http://[cloundfrontid].cloudfront.net/Content/css?v=muhFMZ4thy_XV3dMI2kPt-8Rljm5PNW0tHeDkvenT0g1

现在我只需要被相对于指着我的CDN绝对链接改变由打捆产生的链接。

 &LT;链接HREF =[INSERT_CDN_URL_HERE] /内容/ CSS V = muhFMZ4thy_XV3dMI2kPt-8Rljm5PNW0tHeDkvenT0g1?的rel =stylesheet属性/&GT;

我觉得它可能会重写使用IBundleTransform的路径,但我找不到任何的例子。

请注意:
只是要清楚,我知道你可以指定一个CDN链接捆绑,但只有包可以通过静态链接替换工作。


解决方案

我刚安装MaxCDN跑进了相同的问题。

如你所知,在 bundles.UseCdn 属性,因为我们不希望有指定包的准确网址不理想。像马克斯CDN CDN的是完全相同的网址,查询字符串以及所有,除了一个不同的子域。

下面是我最终解决它。

我创建了一个 BundleHelper 类,将包裹渲染方法,然后prePEND与CDN子域名的路径。

下面是类的样子:

 命名空间MyDomain.Web.Helpers
{
    公共类BundleHelper
    {
        公共静态字符串CdnPath =htt​​p://cdn.mydomain.com;        公共静态IHtmlString的renderScript(字符串路径)
        {
            VAR选择= System.Web.Optimization.Scripts.Render(路径);
            字符串htmlString = HttpUtility.HtmlDe code(opt.ToHtmlString());            如果(BundleTable.EnableOptimizations)
            {
                htmlString = htmlString.Replace(&下; SCRIPT SRC = \\/,的String.Format(&下; SCRIPT SRC = \\{0} /,CdnPath));
            }            返回新HtmlString(htmlString);
        }        公共静态IHtmlString RenderStyle(字符串路径)
        {
            VAR选择= System.Web.Optimization.Styles.Render(路径);
            字符串htmlString = HttpUtility.HtmlDe code(opt.ToHtmlString());            如果(BundleTable.EnableOptimizations)
            {
                htmlString = htmlString.Replace(&LT;链接的href = \\/的String.Format(&LT;链接的href = \\{0} /,CdnPath));
            }            返回新HtmlString(htmlString);
        }
    }
}

然后用它的意见,我简单地做:

  @ BundleHelper.RenderStyle(〜/内容/ CSS)
@ BundleHelper.RenderStyle(〜/内容/主题/基/ CSS)@ BundleHelper.RenderScript(〜/包/ jQuery的)
@ BundleHelper.RenderScript(〜/包/ jQueryUI的)

希望这有助于。

Using the built in MVC4 bundler, how do I prepend my CDN url to the link tags it produces? I've setup Amazon Cloudfront so that it pulls assets from my webserver when first requested. So when I define a bundle like so:

 bundles.Add(new StyleBundle("~/Content/css").Include(
    "~/Content/reset.css",
    "~/Content/960_24_col.css",
    "~/Content/Site.css"
 ));

When deployed, I can reference it thus:

http://[cloundfrontid].cloudfront.net/Content/css?v=muhFMZ4thy_XV3dMI2kPt-8Rljm5PNW0tHeDkvenT0g1

Now I just need to change the links produced by the bundler from being relative to absolute links pointing to my CDN.

  <link href="[INSERT_CDN_URL_HERE]/Content/css?v=muhFMZ4thy_XV3dMI2kPt-8Rljm5PNW0tHeDkvenT0g1" rel="stylesheet"/>

I think it may be possible to rewrite the path using IBundleTransform but I can't find any examples of this.

NOTE: Just to be clear, I know you can specify a CDN link for a bundle, but that only works if the bundle can be replaced by a static link.

解决方案

I just setup MaxCDN and ran into the same exact issue.

As you know, the bundles.UseCdn property is not ideal because we don't want to have to specify the exact url for the bundle. A CDN like Max CDN is the same exact url, query string and all, except for a different subdomain.

Here is how I ended up solving it.

I created a BundleHelper class that will wrap the render method and then prepend the path with the CDN subdomain.

Here is what the class looks like:

namespace MyDomain.Web.Helpers
{
    public class BundleHelper
    {
        public static string CdnPath = "http://cdn.mydomain.com";

        public static IHtmlString RenderScript(string path)
        {
            var opt = System.Web.Optimization.Scripts.Render(path);
            string htmlString = HttpUtility.HtmlDecode(opt.ToHtmlString());

            if (BundleTable.EnableOptimizations)
            {
                htmlString = htmlString.Replace("<script src=\"/", String.Format("<script src=\"{0}/", CdnPath));
            }

            return new HtmlString(htmlString);
        }

        public static IHtmlString RenderStyle(string path)
        {
            var opt = System.Web.Optimization.Styles.Render(path);
            string htmlString = HttpUtility.HtmlDecode(opt.ToHtmlString());

            if (BundleTable.EnableOptimizations)
            {
                htmlString = htmlString.Replace("<link href=\"/", String.Format("<link href=\"{0}/", CdnPath));
            }

            return new HtmlString(htmlString);
        }
    }
}

Then to use it in the views, I simply do:

@BundleHelper.RenderStyle("~/Content/css")
@BundleHelper.RenderStyle("~/Content/themes/base/css")

@BundleHelper.RenderScript("~/bundles/jquery")
@BundleHelper.RenderScript("~/bundles/jqueryui")

Hope this helps.

这篇关于prePEND CDN网址为MVC 4打捆输出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆