捕获并记录关于“缩小失败的MVC4消息。返回未分析的内容”

时间:2013-09-19 23:43:32

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

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

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

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

1 个答案:

答案 0 :(得分:3)

因此,逻辑实际上是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;
    }