ASP.NET MVC 4 - Add Bundle from Controller?

£可爱£侵袭症+ 提交于 2019-12-10 15:23:30

问题


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);

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>
     }
 }

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?

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" />
}

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

@RenderSection("viewStyles", required: false)

回答1:


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")
}

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.



来源:https://stackoverflow.com/questions/16325027/asp-net-mvc-4-add-bundle-from-controller

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!