【问题标题】:Catching and logging MVC4 message about "Minification failed. Returning unminified contents"捕获并记录有关“缩小失败。返回未缩小的内容”的 MVC4 消息
【发布时间】:2013-09-25 05:57:34
【问题描述】:

StackOverflow 上有很多关于从 MVC4 缩小中获取 Minification failed. Returning unminified contents 错误的问题。

我想知道是否有办法在此错误发生时得到通知并能够记录它。

很高兴,当出现错误时,捆绑器会返回原始内容,这样我的网站就不会中断,但我想自动了解这些错误,而不是必须访问每个 css/js 捆绑 url 来查看是否有一个错误。

【问题讨论】:

  • @MikeSmithDev,我的意思是缩小器足够好,可以在出现语法错误时返回原始 CSS,因此不会破坏网站

标签: asp.net-mvc-4 bundling-and-minification asp.net-optimization


【解决方案1】:

因此,该逻辑实际上是在 Script/StyleBundle 正在使用的默认转换的实现中。如果您想自己捕获这些错误,可以将捆绑包上的转换更改为显示这些错误的内容:

因此,要实际检测错误,您必须手动枚举所有捆绑包(以触发它们生成),并且还能够侦听发生的错误(因此下面的 GenerateErrorResponse 等效项需要报告任何错误到您会看到的某个地方)

以下是 JsMinify 在其过程中所做的供参考:

    /// <summary>
    /// Transforms the bundle contents by applying javascript minification
    /// </summary>
    /// <param name="context">The <see cref="BundleContext"/> object that contains state for both the framework configuration and the HTTP request.</param>
    /// <param name="response">A <see cref="BundleResponse"/> object containing the bundle contents.</param>
    public virtual void Process(BundleContext context, BundleResponse response) {
        if (!context.EnableInstrumentation) {
            Minifier min = new Minifier();
            // NOTE: Eval immediate treatment is needed for WebUIValidation.js to work properly after minification
            // NOTE: CssMinify does not support important comments, so we are going to strip them in JS minification as well
            string minifiedJs = min.MinifyJavaScript(response.Content, new CodeSettings() { EvalTreatment = EvalTreatment.MakeImmediateSafe, PreserveImportantComments = false });
            if (min.ErrorList.Count > 0) {
                GenerateErrorResponse(response, min.ErrorList);
            }
            else {
                response.Content = minifiedJs;
            }
        }

        response.ContentType = JsContentType;
    }

【讨论】:

  • 我希望有一个设置来切换错误是否冒泡/暴露而不是重写代码。是否有另一种方法可以查看响应内容并查找“缩小失败。返回未缩小的内容”。错误信息?我认为 Response.Filter 可能能够做到这一点。
猜你喜欢
  • 2014-08-15
  • 2014-01-29
  • 2013-10-22
  • 1970-01-01
  • 1970-01-01
  • 2018-10-19
  • 1970-01-01
  • 1970-01-01
  • 2012-02-12
相关资源
最近更新 更多