运行时动态捆绑和缩小文件在MVC 4 [英] Runtime dynamic bundling and minifying in MVC 4

查看:155
本文介绍了运行时动态捆绑和缩小文件在MVC 4的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道是否有人能帮助我捆绑,并使用附带MVC 4新的优化空间与缩小。
我有一个多租户应用程序中,我要决定哪些JS文件应根据每个用户的设置进行加载。一种方法是创建所有前期束并改变根据用户的设置resolvebundleurl的虚拟路径,但感觉没有真正的正确途径。
我也有基于用户设置,我想在运行时已经缩小的一个CSHTML视图动态CSS。

I was wondering if anybody can help me with bundling and minifying using the new optimization namespace shipped with MVC 4. I have a Multitenant-application in which I want to decide which js files should be loaded based on settings per user. One approach would be to create all bundles upfront and change the virtual path of resolvebundleurl based on the setting of the user, but that feels not really the right way. Also I have dynamic css in a cshtml view based on user-settings, which I would like to have minified in runtime.

有什么建议?我也看到很多其他问题的反应,以检查出Requestreduce的,但他们都是来自同一用户。

Any suggestions? I also see a lot of reactions in other questions to check out Requestreduce, but they are all from the same user.

什么是处理这两种情况下,最好的办法?

What would be the best approach to handle both situations?

在此先感谢!

推荐答案

您可以采取动态构建捆绑应用程序启动时的一种方法。所以,如果你的脚本位于〜/脚本你可以这样做:

One approach you can take is building the bundle dynamically when the application starts. So if your scripts are located in ~/scripts you can do:

Bundle bundle = new Bundle("~/scripts/js", new JsMinify());

if (includeJquery == true) {     
  bundle.IncludeDirectory("~/scripts", "jquery-*");
  bundle.IncludeDirectory("~/scripts", "jquery-ui*");
} 

if (includeAwesomenes == true) {
  bundle.IncludeDirectory("~/scripts", "awesomeness.js");
}

BundleTable.Bundles.Add(bundle);

那么您的标记可以像这样

Then your markup can look like this

@ Scripts.Render(〜/脚本/利布斯/ JS)

注意:我使用的是最新的NuGet包system.web.optimization(现Microsoft.AspNet.Web.Optimization)位于的此处。斯科特Hanselman有一个很好的帖子一下吧。

Note: I'm using the latest nuget package for system.web.optimization (now Microsoft.AspNet.Web.Optimization) located here. Scott Hanselman has a good post about it.

这篇关于运行时动态捆绑和缩小文件在MVC 4的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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