ASP.NET MVC 4-从控制器添加捆绑软件? [英] ASP.NET MVC 4 - Add Bundle from Controller?

查看:61
本文介绍了ASP.NET MVC 4-从控制器添加捆绑软件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的网站上有一些页面使用某些CSS和JS资源-但是它们是使用该CSS或JS文件的唯一页面-因此,我不想在每个页面中都包含CSS和JS参考页。我认为可以修改控制器中的捆绑包并将其添加到已注册的捆绑包中,然后再将其包含在捆绑包引用中,但这并不意味着我可以修改每个View来引用所需的CSS / JS。

I have some pages on my site that use certain CSS and JS resources - but they are the only page(s) to use that css or js file - so I don't want to include that CSS and JS reference in every page. Rather than modify each View to reference the CSS/JS it needs, I thought I could create a bundle in the Controller and add it to the Bundles that are already registered, and then it would be included in the bundle references, but this does not appear to be possible, or maybe I'm just going about it the wrong way.

例如,在我的控制器的注册页面中,我想我可以这样写:

In my Controller for a registration page for example, I thought I could write this:

Bundle styleBundle = new Bundle("~/bundles/registrationStyleBundle");
styleBundle.Include("~/Content/Themes/Default/registration.css");
BundleTable.Bundles.Add(styleBundle);

然后将其保存在我的/Views/Shared/_Layout.cshtml中:

And then have this in my /Views/Shared/_Layout.cshtml:

@foreach(Bundle b in BundleTable.Bundles)
 {
     if (b is StyleBundle)
     {
            <link href="@BundleTable.Bundles.ResolveBundleUrl(b.Path)" rel="stylesheet" type="text/css" />
     }
     else if (b is ScriptBundle)
     {
            <script src="@BundleTable.Bundles.ResolveBundleUrl(b.Path)" type="text/javascript"></script>
     }
 }

但这不起作用-唯一得到的捆绑呈现给我的页面的最终结果是我在 RegisterBundles (在/App_Start/BundleConfig.cs中)中指定的

But this does not work - the only bundles to get rendered to my page end up being the ones I specified in RegisterBundles (in /App_Start/BundleConfig.cs)

知道如何实现这种动态或运行时捆绑吗?

Any idea how to achieve this kind of "dynamic" or "runtime" bundling?

编辑:按照Jasen的建议,我最终做了什么正在从控制器中取出捆绑包创建/注册代码,并将其添加到/App_Start/BundleConfig.cs中的RegisterBundles()中。这样,捆绑包已经可用并且内容最小化了。因此:

EDIT: Following Jasen's advice, what I ended up doing was taking the bundle creation/registration code out of the controller and adding it to RegisterBundles() in /App_Start/BundleConfig.cs. This way, the bundle is already available and the contents get minified. So:

bundles.Add(
new StyleBundle("~/bundles/registrationStyleBundle")
.Include("~/Content/Themes/default/registration.css"));

然后,在我看来,我添加了以下内容:

Then, in my view, I added this:

@section viewStyles{
    <link href="@BundleTable.Bundles.ResolveBundleUrl("~/bundles/registrationStyleBundle")." rel="stylesheet" type="text/css" />
}

然后在/Views/Shared/_Layout.cshtml中添加:

Then, in /Views/Shared/_Layout.cshtml, I added this:

@RenderSection("viewStyles", required: false)


推荐答案

使用 @section脚本{} 块有条件地添加包。

Use the @section Scripts { } block to conditionally add bundles.

_Layout.cshtml

<body>
    ...
@RenderSection("Scripts", required: false)
</body>

FooView.cshtml

@section Scripts {
    @Scripts.Render("~/bundles/foo")
}

KungFooView.cshtml

@section Scripts {
    @Scripts.Render("~/bundles/kungfoo")
}

在我的BundleConfig中,我通常将资源分组

In my BundleConfig I typically group resources

bundles.Add(new ScriptBundle("~/bundles/Areas/Admin/js").Include(...);
bundles.Add(new StyleBundle("~/bundles/Areas/Admin/css").Include(...);
bundles.Add(new ScriptBundle("~/bundles/Areas/Home/js").Include(...);
bundles.Add(new StyleBundle("~/bundles/Areas/Home/css").Include(...);

现在我可以定义多个布局文件,也可以选择添加捆绑查看意见。

Now I can either define multiple layout files or just selectively add bundles to the views.

这篇关于ASP.NET MVC 4-从控制器添加捆绑软件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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