ASP.NET MVC 4使用束对Url.Content好处 [英] ASP.NET Mvc 4 Use bundle's benefits for Url.Content

查看:164
本文介绍了ASP.NET MVC 4使用束对Url.Content好处的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有什么办法可以做到这一点?

Is there any way I can do this?

一些捆绑销售的好处是:

Some of the benefits of bundling are:


  • 最小化

  • Gzip已融为一体pression

  • 请求有处理文件versiones(缓存)令牌参数。

在我的地盘我用了很多捆,但在一些网页我只有1剧本,我不认为我应该只为1脚本创建一个包。有没有什么办法可以使用​​带有 Url.Content这三个好处方法。

In my site I use a lot of bundles, but in some pages I only have 1 script and I don't think I should create a bundle only for 1 script. Is there any way I can use this three benefits with Url.Content method.

我的乌托邦的解决办法是建立东西(也许在web.config),每当Url.Content把它叫做添加此功能。在任一方式本使用它:

My utopic solution would be to set up something (maybe in the web.config) and whenever Url.Content is called it adds this functionality. Using it in either of this ways:

<script type="text/javascript" src="@Url.Content("~/Scripts/...")"></script>
<script type="text/javascript" src="~/Scripts/..."></script>

(第二个是因为我使用的剃刀2

如果这是不可能的,我可以做一个扩展方法UrlHelper添加此功能。

If that is not possible I can make an extension method to UrlHelper to add this functionality.

谢谢!

推荐答案

有什么真的错用一个文件创建一个包获得的缩小和版本的好处。你将不得不使用Scripts.Render帮助为好,有一个在UrlHelper此不支持当前,但正如你已经提到你可以写一个扩展方法调用到脚本帮手。

There's nothing really wrong with creating a bundle with one file to get the benefits of minification and versioning. You would have to use the Scripts.Render helper as well, there's no support for this in the UrlHelper currently, but as you mentioned already you could write an extension method to call into the Scripts helper.

升级(OP)

下面是我对任何扩展方法谁想要使用它:

Here are my extension method for anyone who want to use it:

public static IHtmlString DynamicScriptsBundle(this HtmlHelper htmlHelper, string nombre, params string[] urls)
{
    string path = string.Format("~/{0}", nombre);
    if (BundleTable.Bundles.GetBundleFor(path) == null)
        BundleTable.Bundles.Add(new ScriptBundle(path).Include(urls));
    return Scripts.Render(path);
}

public static IHtmlString DynamicStylesBundle(this HtmlHelper htmlHelper, string nombre, params string[] urls)
{
    string path = string.Format("~/{0}", nombre);
    if (BundleTable.Bundles.GetBundleFor(path) == null)
        BundleTable.Bundles.Add(new StyleBundle(path).Include(urls));
    return Styles.Render(path);
}

这篇关于ASP.NET MVC 4使用束对Url.Content好处的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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