ASP MVC资源解释为样式表,但使用MIME类型application / javascript进行传输

时间:2017-09-27 08:45:19

标签: asp.net-mvc google-chrome mime-types

我习惯于引用我的所有cssjs文件,而不会像这样捆绑:

<link rel='stylesheet' type='text/css' href='style.css'/>

但是由于我使用Bundling,我在chrome中收到以下错误消息:

  

资源解释为样式表但使用MIME类型application / javascript传输:“http://localhost:9432/Assets/global/plugins/bootstrap/js/bootstrap.min.js”。

我发现我应该将Content-Type设置为application/javascript,但我不知道如何做到这一点。

您的建议是什么解决了这个问题?

更新

这是我的代码:

          bundles.Add(new StyleBundle("~/bundles/AdminStyle").Include(
                  "~/Assets/global/plugins/font-awesome/css/font-awesome.min.css",
                  "~/Assets/global/plugins/simple-line-icons/simple-line-icons.min.css",
                  "~/Assets/global/plugins/bootstrap/css/bootstrap-rtl.min.css",
                  "~/Assets/global/plugins/bootstrap-switch/css/bootstrap-switch-rtl.min.css",
                  "~/Assets/global/plugins/bootstrap-select/css/bootstrap-select-rtl.css",
                  "~/Assets/global/css/components-rounded-rtl.min.css",
                  "~/Assets/global/css/plugins-rtl.min.css",
                  "~/Assets/layouts/layout3/css/layout-rtl.min.css",
                  "~/Assets/layouts/layout3/css/themes/default-rtl.min.css",
                  "~/Assets/layouts/layout3/css/custom.css",
                  "~/Assets/global/plugins/bootstrap-social-button/css/bootstrap-social.css",
                  "~/Assets/global/plugins/bootstrap-toastr/toastr.min.css",
                  "~/Assets/global/css/Custom.css",
                  "~/Assets/global/css/helper.css"));

并且

 bundles.Add(new StyleBundle("~/bundles/AdminScript").Include(
      "~/Assets/global/plugins/bootstrap/js/bootstrap.min.js",
      "~/Assets/global/plugins/js.cookie.min.js",
      "~/Assets/global/plugins/jquery-slimscroll/jquery.slimscroll.min.js",
      "~/Assets/global/plugins/jquery.blockui.min.js",
      "~/Assets/global/plugins/bootstrap-switch/js/bootstrap-switch.min.js",
      "~/Assets/global/scripts/app.min.js",
      "~/Assets/layouts/layout3/scripts/layout.min.js",
      "~/Assets/layouts/layout3/scripts/demo.min.js",
      "~/Assets/layouts/global/scripts/quick-sidebar.min.js",
      "~/Assets/layouts/global/scripts/quick-nav.min.js",
      "~/Assets/global/plugins/bootstrap-select/js/bootstrap-select.min.js"));

2 个答案:

答案 0 :(得分:3)

您正在将js文件呈现为<link rel="stylesheet" href="/bundles/....">

因此,在cshtml中,将@Styles.Render("~/bundles/AdminScript")更改为@Scripts.Render("~/bundles/AdminScript")

答案 1 :(得分:1)

在我的情况下,我的问题是我使用ScriptBundle,而应该使用StyleBundle

代码错误:

        bundles.Add(new ScriptBundle("~/Content/rating").Include(
                "~/Content/fontawesome-stars.css"));

正确的代码:

        bundles.Add(new StyleBundle("~/Content/rating").Include(
                "~/Content/fontawesome-stars.css"));

您必须小心使用这些捆绑软件,因为它可以完美地编译并可以在本地计算机上运行,​​而不会出现问题,但是在生产中会出现所有问题!