如何在 ASP.NET MVC 4 Beta 中禁用 Javascript/CSS 缩小
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9373071/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to disable Javascript/CSS minification in ASP.NET MVC 4 Beta
提问by Jeff
I am just trying out ASP.NET MVC 4 but I can't figure out how to disable Javascript/CSS minification feature. Especially for development environment this will help greatly on debugging. I would imagine it would be a switch in web.config but since ASP.NET MVC 4 is still in beta stage at the moment there's really not much information out there. Would appreciate if someone can help or point to the right blog posts etc.
我只是在尝试 ASP.NET MVC 4,但我不知道如何禁用 Javascript/CSS 缩小功能。特别是对于开发环境,这对调试有很大帮助。我想这将是 web.config 中的一个开关,但由于 ASP.NET MVC 4 目前仍处于测试阶段,所以那里的信息真的不多。如果有人可以提供帮助或指向正确的博客文章等,我们将不胜感激。
回答by Socardo
In Global.asax.cs
在 Global.asax.cs
#if DEBUG
foreach (var bundle in BundleTable.Bundles)
{
bundle.Transform = new NoTransform();
}
#endif
回答by Russell Durham
Another option would be to create an HTML Helper that you could use to build the script and link tags. Here is what I have implemented for the Javascript, which can also be done for the CSS:
另一种选择是创建一个 HTML 帮助程序,您可以使用它来构建脚本和链接标签。这是我为 Javascript 实现的,也可以为 CSS 实现:
public static class BundleHelper
{
public static MvcHtmlString JsBundle(this HtmlHelper helper, string bundlePath)
{
var jsTag = new TagBuilder("script");
jsTag.MergeAttribute("type", "text/javascript");
return ReferenceBundle(helper, bundlePath, jsTag);
}
public static MvcHtmlString ReferenceBundle(this HtmlHelper helper, string bundlePath, TagBuilder baseTag)
{
var httpContext = helper.ViewContext.HttpContext;
var urlHelper = new UrlHelper(helper.ViewContext.RequestContext);
Bundle bundle = BundleTable.Bundles.GetBundleFor(bundlePath);
var htmlString = new StringBuilder();
if (bundle != null)
{
var bundleContext = new BundleContext(helper.ViewContext.HttpContext, BundleTable.Bundles, urlHelper.Content(bundlePath));
if (!httpContext.IsDebuggingEnabled)
{
baseTag.MergeAttribute("href", System.Web.Optimization.BundleTable.Bundles.ResolveBundleUrl(bundlePath));
return new MvcHtmlString(baseTag.ToString());
}
foreach (var file in bundle.EnumerateFiles(bundleContext))
{
var basePath = httpContext.Server.MapPath("~/");
if (file.FullName.StartsWith(basePath))
{
var relPath = urlHelper.Content("~/" + file.FullName.Substring(basePath.Length));
baseTag.MergeAttribute("href", relPath, true);
htmlString.AppendLine(baseTag.ToString());
}
}
}
return new MvcHtmlString(htmlString.ToString());
}
}
Now all that you have to do is call it in your view:
现在您所要做的就是在您的视图中调用它:
<head>
<meta charset="utf-8" />
<title>@ViewBag.Title - My ASP.NET MVC Application</title>
<link href="~/favicon.ico" rel="shortcut icon" type="image/x-icon" />
<link href="~/Content/css" rel="stylesheet" type="text/css" />
<link href="~/Content/themes/base/css" rel="stylesheet" type="text/css" />
@Html.JsBundle("~/scripts/js")
<meta name="viewport" content="width=device-width" />
</head>
And it will render the scripts as separate references, or use the new bundling/minification feature depending on what the debug setting is in your web.config . I used some of the code from http://codecutout.com/resource-minify-bundlingas a reference when creating my helper if you wanted to see some more examples. Their helper is written a little better, throwing exceptions when invalid arguments are supplied, etc.... I just haven't gotten around to cleaning mine up yet.
它会将脚本呈现为单独的引用,或者根据 web.config 中的调试设置使用新的捆绑/缩小功能。如果您想查看更多示例,我在创建助手时使用了来自http://codecutout.com/resource-minify-bundling 的一些代码作为参考。他们的助手写得好一点,当提供无效参数时抛出异常,等等......我只是还没有来得及清理我的。
回答by Wesley Bakker
You could register your own bundles in the Global.asaxand use the NoTransform
class if you do not want the content to be minified.
如果您不想缩小内容,您可以在Global.asax 中注册您自己的包并使用NoTransform
该类。
I personally don't want my script to be transformed at all. I just create two script directories. One with the debug script versions and one with the originally downloaded minified versions.
我个人根本不希望我的脚本被转换。我只是创建了两个脚本目录。一种带有调试脚本版本,一种带有最初下载的缩小版本。
The MVC 4 out of the box minifier (JsMinify) breaks jQuery 1.7.1 for Opera, so I do not want to use that one. I just put the following lines in my Global.asax: Application_Start()
method:
MVC 4 开箱即用的缩小器 (JsMinify) 破坏了 Opera 的 jQuery 1.7.1,所以我不想使用那个。我只是在我的Global.asax:Application_Start()
方法中添加了以下几行:
Bundle debugScripts = new Bundle("~/DebugScripts",
new NoTransform("text/javascript"));
debugScripts.AddDirectory("~/Scripts/Debug", "*.js");
BundleTable.Bundles.Add(debugScripts);
Bundle productionScripts = new Bundle("~/ProductionScripts",
new NoTransform("text/javascript"));
productionScripts.AddDirectory("~/Scripts/Minified", "*.js");
BundleTable.Bundles.Add(productionScripts);
With that in place I can simply add either one of two lines in my _layouts.cshtml
:
有了这个,我可以简单地在我的_layouts.cshtml
以下两行中添加任何一行:
<script src="@System.Web.Optimization.BundleTable.Bundles.ResolveBundleUrl("~/DebugScripts")" type="text/javascript"></script>
<script src="@System.Web.Optimization.BundleTable.Bundles.ResolveBundleUrl("~/ProductionScripts")" type="text/javascript"></script>
Of course we could get a little more funky with this in place. We could generate just one bundle and depending on the built type select what files to include.
当然,有了这个,我们可以变得更加时髦。我们可以只生成一个包,并根据构建类型选择要包含的文件。
回答by Ian Mercer
After the call to EnableDefaultBundles()
in Global.asax, you can do this ...
EnableDefaultBundles()
在 Global.asax 中调用后,您可以执行此操作...
if ( ... running in development environment ...)
{
var registeredBundles = BundleTable.Bundles.GetRegisteredBundles();
foreach (var bundle in registeredBundles)
{
if (bundle.Transform is System.Web.Optimization.JsMinify)
bundle.Transform = new NoTransform();
}
}
Not pretty (modifying state set by the system), but it's a lot less code than all the other suggestions, still lets you use the standard bundling behavior and it doesn't involve any changes to your views.
不漂亮(修改系统设置的状态),但它比所有其他建议的代码少得多,仍然允许您使用标准的捆绑行为,并且不涉及对您的视图进行任何更改。
回答by Roman Pushkin
On newer versions of ASP.NET MVC just add
在较新版本的 ASP.NET MVC 上只需添加
#if DEBUG
foreach (var bundle in BundleTable.Bundles)
{
bundle.Transforms.Clear();
}
#endif
right after
紧随其后
BundleConfig.RegisterBundles(...);
回答by Jon
you can turn it off from config:
您可以从配置中关闭它:
<system.web>
<compilation debug="true" />
<!-- Lines removed for clarity. -->
</system.web>
http://www.asp.net/mvc/tutorials/mvc-4/bundling-and-minification
http://www.asp.net/mvc/tutorials/mvc-4/bundling-and-minification
回答by resnyanskiy
I think it would be right, if such feature will be available "out of the box".
我认为这是正确的,如果这样的功能可以“开箱即用”。
I posted a feedback on UserVoice.com: http://aspnet.uservoice.com/forums/41201-asp-net-mvc/suggestions/2702000-improve-system-web-optimization-bundle
我在 UserVoice.com 上发布了一个反馈:http: //aspnet.uservoice.com/forums/41201-asp-net-mvc/suggestions/2702000-improve-system-web-optimization-bundle
Give it your "voices".
给它你的“声音”。
回答by Eric J.
Rather than replace instances of JsMinify and CssMinify, one can instead use interfaces. This option was not available in earlier releases because the second constructor parameter was a type rather than an interface.
与其替换 JsMinify 和 CssMinify 的实例,不如使用接口。此选项在早期版本中不可用,因为第二个构造函数参数是类型而不是接口。
IBundleTransform jsTransform;
IBundleTransform cssTransform;
#if DEBUG
jsTransform = new NoTransform("text/javascript");
cssTransform = new NoTransform("text/css");
#else
jsTransform = new JsMinify();
cssTransform = new CssMinify();
#endif
Bundle jsBundle = new Bundle("~/JsB", jsTransform);
Bundle cssBundle = new Bundle("~/CssB", cssTransform);
Perhaps also worth noting, for scripts that are shipped with minified and non-minified versions e.g. jQuery, one can use a helper method to optionally strip out the ".min" for DEBUG builds to facilitate debugging:
也许还值得注意的是,对于带有缩小和非缩小版本(例如 jQuery)的脚本,可以使用辅助方法选择性地去除 DEBUG 构建的“.min”以方便调试:
private string Min(string scriptNameIncludingMin)
{
#if DEBUG
return scriptNameIncludingMin.Replace(".min", ""); // Remove .min from debug builds
#else
return scriptNameIncludingMin;
#endif
}
// ...
jsBundle.AddFile(Min("~/Scripts/jquery-1.7.2.min.js"));
回答by Andrey Taritsyn
Try a new extension for System.Web.Optimization - Bundle Transformer. In Bundle Transformer implemented a number of opportunities to simplify debugging (see documentation).
尝试 System.Web.Optimization - Bundle Transformer的新扩展。在 Bundle Transformer 中实现了许多简化调试的机会(请参阅文档)。
回答by Chris S
Another alternative (tested with v1.1.0.0 and MVC5):
另一种选择(使用 v1.1.0.0 和 MVC5 测试):
public class BundleConfig
{
public static void Register()
{
ScriptBundle jsBundle = new ScriptBundle("~/Scripts/myscript.min.js");
jsBundle.Include("~/Scripts/myscript.js");
DisableInDebugMode(jsBundle);
BundleTable.Bundles.Add(jsBundle);
}
private static void DisableInDebugMode(ScriptBundle jsBundle)
{
#if DEBUG
// Don't minify in debug mode
jsBundle.Transforms.Clear();
#endif
}
}