2

在我开始之前,让我说我知道关于这个问题还有其他关于这个问题的问题有很好的答案,但请让我解释一下我的不同之处。

我有一个 ASP.NET MVC 5 项目,其中有一组捆绑包:

public class BundleConfig
{
    // For more information on bundling, visit http://go.microsoft.com/fwlink/?LinkId=301862
    public static void RegisterBundles(BundleCollection bundles)
    {
        bundles.Add(new ScriptBundle("~/bundles/jquery").Include(
                    "~/Scripts/jquery.js"));

        bundles.Add(new ScriptBundle("~/bundles/jqueryval").Include(
                    "~/Scripts/jquery.validate*"));

        // Use the development version of Modernizr to develop with and learn from. Then, when you're
        // ready for production, use the build tool at http://modernizr.com to pick only the tests you need.
        bundles.Add(new ScriptBundle("~/bundles/modernizr").Include(
                    "~/Scripts/modernizr-*"));


        bundles.Add(new ScriptBundle("~/bundles/bootstrap").Include(
            "~/Scripts/bootstrap.js"));

        bundles.Add(new StyleBundle("~/Content/css").Include(
                  "~/Content/bootstrap.css",
                  "~/Content/freelancer.css"));

        // Plugin JavaScript
        bundles.Add(new ScriptBundle("~/bundles/pluginjs").Include(
                "~/Scripts/cbpAnimatedHeader.js",
                "~/Scripts/classie.js"));

        //Custom Theme js
        bundles.Add(new ScriptBundle("~/bundles/customthemejs").Include(
                    "~/Scripts/freelancer.js"));

        // Contact Form JavaScript
        bundles.Add(new ScriptBundle("~/bundles/contactvalidation").Include(
                  "~/Content/jqBootstrapValidation.js",
                  "~/Content/contact_me.js"));
    }

这些被称为_Layout.cshtml

    @Scripts.Render("~/bundles/customthemejs")
    @Scripts.Render("~/bundles/jquery")
    @Scripts.Render("~/bundles/contactvalidation")
    @Scripts.Render("~/bundles/pluginjs")
    @Scripts.Render("~/bundles/bootstrap")
    @RenderSection("scripts", required: false)   
</body>
</html>

在`global.asax中:

protected void Application_Start()
    {
        AreaRegistration.RegisterAllAreas();
        FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
        RouteConfig.RegisterRoutes(RouteTable.Routes);
        BundleConfig.RegisterBundles(BundleTable.Bundles);
    }

到目前为止,这是我所知道的:

  1. 所有StyleBundle's 都被成功调用和调用,因此我可以将问题定位到脚本引用中的某些内容
  2. 引用的脚本也位于正确的文件夹中,即 ~/Scripts/.... 所以我知道这不是引用问题。
  3. 这些脚本正在工作,因为我已经使用 Web 表单项目对其进行了测试
  4. 我已尝试引用head部分中的捆绑包,_Layout.cshtml但没有更改
  5. chrome 中的开发工具和 VS 中的调试器显示脚本未运行
  6. 我唯一的怀疑是,在课堂上的呼叫_Layout.cshtml和捆绑包之间存在差距BundleConfig

我已经尝试过其他人在网络上推荐的其他解决方案,例如查找语法错误等……但据我所知,没有。

为了使脚本的结构明显,我包含了一个屏幕截图: 在此处输入图像描述

谁能从与我不同的角度看待这一点,看看我哪里出错了?

4

1 回答 1

2

您的代码看起来正确,但您引用Microsoft.Web.Optimization的应用程序config可能无法正常工作。您可以强制引用,以便应用程序使用Web.Optimization. 在您_Layout.cshtml的捆绑数据上方,放置以下内容:

@using System.Web.Optimization

那应该正确执行Web.Optimization.

我们看不到的唯一可能错误的部分是您的 Global.asax。您需要确保调用RegisterBundle.

BundleConfig.RegisterBundles(BundleTable.Bundles);
于 2015-09-26T03:08:59.583 回答