一般只要js文件裡面代碼錯誤,System.Web.Optimization就不會自動壓縮了,現在擴展了兩個類,重寫了下方法,設置為忽略js報錯,也能繼續壓縮js代碼了

using Microsoft.Ajax.Utilities;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.Optimization;
using DouglasCrockford.JsMin;

namespace WebApplicationTestJs.Models
{
    public class JsMinify2 : IBundleTransform//JsMinify
    {
        /// <summary>
        /// 是否忽略js代碼錯誤
        /// </summary>
        public bool CodeIgnoreAllErrors { get; set; }
        protected static void GenerateErrorResponse(BundleResponse bundle, IEnumerable<object> errors)
        {
            StringBuilder stringBuilder = new StringBuilder();
            stringBuilder.Append("/* ");
            //stringBuilder.Append(OptimizationResources.MinifyError).Append("\r\n");
            stringBuilder.Append("js代碼有錯").Append("\r\n");
            foreach (object obj in errors)
            {
                stringBuilder.Append(obj.ToString()).Append("\r\n");
            }
            stringBuilder.Append(" */\r\n");
            stringBuilder.Append(bundle.Content);
            bundle.Content = stringBuilder.ToString();
        }
        /// <summary>
        /// 重寫此方法,忽略js報錯,繼續壓縮
        /// </summary>
        /// <param name="context"></param>
        /// <param name="response"></param>
        public virtual void Process(BundleContext context, BundleResponse response)
        {
            if (context == null)
            {
                throw new ArgumentNullException("context");
            }
            if (response == null)
            {
                throw new ArgumentNullException("response");
            }
            if (!context.EnableInstrumentation)
            {
                Minifier minifier = new Minifier();
                string content = minifier.MinifyJavaScript(response.Content, new CodeSettings
                {
                    EvalTreatment = EvalTreatment.MakeImmediateSafe,
                    PreserveImportantComments = false,
                    //自定義,設置忽略錯誤
                    //IgnoreAllErrors = true
                    //IgnoreAllErrors= CodeIgnoreAllErrors       
                     TermSemicolons = true,              
                    //將中文轉換為Unicode編碼
                    AlwaysEscapeNonAscii=true            
                });
                if (minifier.ErrorList.Count > 0)
                {
                    //GenerateErrorResponse(response, minifier.ErrorList);                 
                    //JsMinify.GenerateErrorResponse(response, minifier.ErrorList);
                    //用其他組件壓縮,在nuget安裝DouglasCrockford.JsMin組件
                    var minifier3 = new DouglasCrockford.JsMin.JsMinifier();
                    string content2 = minifier3.Minify(response.Content);
                    response.Content = content2;
                }
                else
                {
                    response.Content = content;
                }
            }
            response.ContentType = "text/javascript";// JsMinify.JsContentType;
        }
    }
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Optimization;

namespace WebApplicationTestJs.Models
{
    /// <summary>
    /// 默認js代碼有錯,也壓縮
    /// </summary>
    public class ScriptBundle2 : Bundle
    {
        public ScriptBundle2(string virtualPath) : this(virtualPath, null, true)
        {
        }

        /// <summary>
        /// 構造函數
        /// </summary>
        /// <param name="virtualPath"></param>
        /// <param name="codeIgnoreAllErrors">是否忽略js代碼錯誤</param>
        public ScriptBundle2(string virtualPath, bool codeIgnoreAllErrors=true) : this(virtualPath, null, codeIgnoreAllErrors)
        {
        }

        public ScriptBundle2(string virtualPath, string cdnPath, bool codeIgnoreAllErrors = true) : base(virtualPath, cdnPath, new IBundleTransform[]
        {
            new JsMinify2(){ CodeIgnoreAllErrors=codeIgnoreAllErrors }
        })
        {
            base.ConcatenationToken = ";" + Environment.NewLine;
        }

    }
}

代碼調用

  public class BundleConfig
    {
        public static void RegisterBundles(BundleCollection bundles)
        {
            BundleTable.EnableOptimizations = true;
            //false設置不忽略js代碼錯誤,有錯時,會不壓縮js
            //bool IgnoreAllErrors = false;

            bundles.Add(new ScriptBundle2("~/Content/SiteJs").Include(
                   "~/Content/reservartionOrder.js"
           ));
            //bundles.Add(new StyleBundle("~/Theme/css/default/easyui").Include("~/Theme/css/default/easyui.css"));

        }
    }

碼云項目地址:
https://gitee.com/wanghaoli/CompressionHtml_Js_Css


本文转载:CSDN博客