2

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

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

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

4

1 回答 1

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;
    }
于 2013-09-25T23:38:21.390 回答