asp.net-mvc - 与 MVC4.5 捆绑在发布后不起作用

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

我尝试为我的应用程序捆绑脚本和样式 Bootstrap 。我的调试正常,但是当我发布它时,不加载脚本和样式。

我添加一个 BundleConfig (BootstrapBundleConfig)

public static void RegisterBundles(BundleCollection bundles)
    {
        bundles.Add(new ScriptBundle("~/js").Include(
            "~/Scripts/jquery-{version}.js",
            "~/Scripts/jquery-ui-1.10.4.custom.js",
            "~/Scripts/jquery-migrate-{version}.js",
            "~/Scripts/bootstrap.js",
            "~/Scripts/gridmvc.js",
            "~/Scripts/gridmvc.lang.fa.js",
            "~/Scripts/jquery.validate.js",
            "~/scripts/jquery.validate.unobtrusive.js",
            "~/Scripts/jquery.validate.unobtrusive-custom-for-bootstrap.js",
            "~/scripts/entitypicker.js",
            "~/scripts/js-persian-cal.js",
            "~/scripts/json2.js",
            "~/scripts/bootbox.js",
            "~/Scripts/jalali.js",
            "~/Scripts/calendar.js",
            "~/Scripts/calendar-setup.js",
            "~/Scripts/lang/calendar-fa.js"
            ));
        bundles.Add(new StyleBundle("~/content/css").Include(
             "~/Content/bootstrap.css",
             "~/Content/bootstrap-responsive.css",
             "~/Content/bootstrap-mvc-validation.css",
             "~/Content/themes/ui-lightness/jquery-ui.css",
             "~/Content/js-persian-cal.css",
             "~/Content/entitypicker.css",
             "~/Content/gridmvc.css",
             "~/Content/aqua/theme.css",
             "~/Content/calendar-system.css"
            ));

    }

在全局中添加注册

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

并在_layaout中设置样式/脚本

<link href="@Styles.Url("~/content/css")" rel="stylesheet" />
@Scripts.Render("~/js")

但是发布时,并打开查看页面源

    <link href="/Content/css?v=65vDyivXbF9ucPBBLls9CVnwUcCNass7hOMNUEXbN-I1" rel="stylesheet" />

打开此文件时,第一行出现错误。

/* Minification failed. Returning unminified contents.
(962,1): run-time error CSS1019: Unexpected token, found '@charset'
(962,10): run-time error CSS1019: Unexpected token, found '"UTF-8"'
(962,17): run-time error CSS1019: Unexpected token, found ';'
(994,1): run-time error CSS1019: Unexpected token, found '@-webkit-keyframes'

我在Google中搜索并使用了不同的方法,但错误并未解决。

  1. 将所有样式添加到特殊路径(Content/Them/Bootstrap)并使用new StyleBundle("~/content/Them/Bootstrap")

  2. 使用BundleTable.EnableOptimizations = true;

其他..

最佳答案

如果您的 css 文件格式不正确,通常会发生这种情况,请使用 http://csslint.net/ 将您的 css 代码进行测试.

也不要使用这一行

<link href="@Styles.Url("~/content/css")" rel="stylesheet" />

你可以使用这个

@Styles.Render("~/content/css")

如果您想在不发布的情况下进行调试,您可以将此行添加到 BundleConfigRegisterBundles 方法中:

BundleTable.EnableOptimizations = true;

关于asp.net-mvc - 与 MVC4.5 捆绑在发布后不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24751124/

相关文章:

html - MVC4 项目图像不显示

javascript - 将 JS 错误行号映射到未缩小的非捆绑 Assets 文件

asp.net - 在 ASP.NET 捆绑和缩小中将样式的 URL 转换为 CDN URL

asp.net-mvc - 根据查询字符串/派生 Controller 注入(inject)不同的存储库,并根据 Controller 类型/ASP.NET MVC 注入(inject)存储库

c# - 使用带有可选输入域\用户名或仅用户名的 MVC4 表单根据 Active Directory 对用户进行身份验证

c# - 有人在 Nhibernate 中使用 ASP.NET MembershipProvider 吗?

c# - 当所有 Controller 都继承自 AsyncController 时,这是一个好习惯吗?

c# - 应用程序在闲置 10 分钟后注销用户

javascript - Angular 应用程序在缩小时无法工作(可能是 ng 路线或其他原因)

asp.net-mvc - 当我们设置线程 CurrentCulture/CurrentUICulture 时,是否会影响 ASP.NET 中的多个用户?