【问题标题】:jQuery not minifying in asp.net MVCjQuery 没有在 asp.net MVC 中缩小
【发布时间】:2014-02-14 00:02:01
【问题描述】:

我正在使用 ASP.Net MVC。

由于某种原因,我的 JS 文件没有缩小。我的 CSS 正在缩小。

我的 CSS 缩小为:<link href="/ffee/content/css?v=vaNo49ei5GzRQAYnj-AD-AbQI3GKNs2OZ8q6-oJEEvk1" rel="stylesheet">,但我的所有 JS 文件都在源代码中单独列出。

我正在发布模式下编译和发布。

谁能告诉我如何解决这个问题?

谢谢,

标记

BootstrapBundleConfig.cs

using System.Web;
using System.Web.Mvc;
using System.Web.Optimization;

namespace BootstrapSupport
{
public class BootstrapBundleConfig
{
    public static void RegisterBundles(BundleCollection bundles)
    {
        bundles.Add(new ScriptBundle("~/js").Include(
            "~/Scripts/jquery-{version}.js",
            "~/Scripts/jquery-migrate-{version}.js",
            "~/Scripts/bootstrap.js",
            "~/Scripts/jquery.validate.js",
            "~/scripts/jquery.validate.unobtrusive.js",
            "~/Scripts/jquery.validate.unobtrusive-custom-for-bootstrap.js",
            "~/Scripts/knockout-2.2.0.js",
            "~/Scripts/bootstrap-combobox.js",
             "~/Scripts/select2.js"
            ));

        bundles.Add(new ScriptBundle("~/mt").Include(
           "~/Scripts/bootstrap-datepicker.js",
                            "~/Scripts/mtDateEdit.js",
                            "~/Scripts/jquery-te-1.4.0.js"
                            //"~/Scripts/tinymce/tinymce.js"
                            ));

        bundles.Add(new StyleBundle("~/content/css").Include(
            "~/Content/bootstrap.css",
            "~/Content/body.css",
            "~/Content/bootstrap-responsive.css",
            "~/Content/bootstrap-mvc-validation.css",
            "~/Content/bootstrap-combobox.css",
             "~/Content/select2.css",
            "~/Content/datepicker.css",
            "~/Content/jquery-te-1.4.0.css"
            ));
    }
}
}

Global.asax.cs

   public class MvcApplication : System.Web.HttpApplication
{
    protected void Application_Start()
    {
        AreaRegistration.RegisterAllAreas();

        WebApiConfig.Register(GlobalConfiguration.Configuration);
        FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
        RouteConfig.RegisterRoutes(RouteTable.Routes);
        BundleConfig.RegisterBundles(BundleTable.Bundles);
        AuthConfig.RegisterAuth();

        BootstrapSupport.BootstrapBundleConfig.RegisterBundles(System.Web.Optimization.BundleTable.Bundles);

        BootstrapMvcSample.ExampleLayoutsRouteConfig.RegisterRoutes(RouteTable.Routes);
        RegisterViewMapper();
    }

_Bootstrap.Layout.Basic.cshtml - 在文件底部:

 <div class="container">
     @Scripts.Render("~/js")
     @Scripts.Render("~/mt")
     @RenderSection("Scripts", required: false)

     @Html.Partial("_alerts")
     @Html.Partial("_validationSummary")
     @RenderBody()   

 </div>

【问题讨论】:

  • 你是debug还是release模式?
  • 嗨 - 发布模式。我的 CSS 缩小为: - 但我所有的 JS 文件都在源代码中单独列出。谢谢,马克
  • 你试过用BundleTable.EnableOptimizations = true;强制它
  • 嗨@lalibi - 谢谢你的工作。看起来有缩小错误,这也许就是它不能自动工作的原因。请发布答案,我会这样标记。谢谢你,马克

标签: c# jquery asp.net asp.net-mvc-3


【解决方案1】:

尝试用BundleTable.EnableOptimizations = true;强制它

如您所说,如果存在缩小/捆绑错误,请从捆绑包中一一删除 *.js 文件,直到找到有问题的文件。找到它后,请尝试在此文件的顶部和末尾添加换行符。

【讨论】:

    【解决方案2】:

    您可以强制它仅用于发布模式,只需在 Application_Start 中添加以下代码

     #if RELEASE            
            BundleTable.EnableOptimizations = true;            
     #endif
    

    【讨论】:

      猜你喜欢
      • 2018-06-21
      • 2014-04-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-05-13
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多