mvc4捆绑强类型的包 [英] mvc4 bundling strongly typed bundles

查看:369
本文介绍了mvc4捆绑强类型的包的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以MVC 4引入脚本和样式捆绑。这使得本:

So MVC 4 introduces script and style bundling. Which allows for this:

public static void RegisterBundles(BundleCollection bundles)
    {
    bundles.Add(new ScriptBundle("~/bundles/mobile").Include(
                    "~/Scripts/jquery.mobile-*"));

那么在这样的剃须刀视图中使用:

then used in a razor view like this:

@Scripts.Render("~/bundles/mobile")

我的问题是,为什么我必须键入〜/包/移动?有没有办法让intellisence有一个强类型的对象回暖呢?否则,我必须去看看它,以确保我把它叫做同样的事情。

My question is why do I have to type "~/bundles/mobile"? Is there a way get intellisence to have a strongly typed object to pick up on? Otherwise I have to go look it up to make sure I call it the same thing.

我愿做这样的事情:(我知道这不会编译这种方式,它只是一个例子)

I would like to do something like this: (I know this won't compile this way, it's just an example)

public static void RegisterBundles(BundleCollection bundles)
    {
    Bundle mobile = new Bundle("mobile");
    mobile.AddFile("w/e")
    bundles.Add(mobile);

//in page:
 @Scripts.Render(BundleConfig.mobile)

或东西的影响。

修改:答案很简单。由于 @Hao功夫指出 @ Styles.Render 只是需要一个URL字符串路径。我创建了一个类来保存pathes。

Edit: the answer so simple. As @Hao Kung points out @Styles.Render simply takes a url string path. I created a class to hold the pathes.

public class bundles
{
    #region Javascript
    public static string scripts = "~/bundles/scripts";
    ...
    #endregion

    #region CSS

    public static string css = "~/Content/css";
    public static string jqueryUi = "~/Content/themes/base/css";
    ...
    #endregion
}

在任何页面,那么你只需做

in any page then you simply do

@Styles.Render(bundles.jqueryUi)

你有它。在你一点点额外的努力,但至少我们强烈此时键入

there you have it. A little extra effort on your part, but at least it's strongly typed now.

推荐答案

渲染脚本/样式渲染佣工不限于渲染引用捆绑,他们解决任何网址,所以助手的唯一方法来检测你的意思参考束,是通过使束中的虚拟路径

The Render Scripts/Styles Render helpers are not limited to rendering references to bundles, they resolve any urls, so the only way for the helper to detect that you mean to reference a bundle, is by passing in the virtual path of the bundle.

这篇关于mvc4捆绑强类型的包的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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