2015-09-26 50 views
1

之前,我开始让我说,我知道有SO上关于这个问题有很大的答案,但请让我解释一下我的怎么是不同的其他问题。ASP.NET MVC 5个捆绑的脚本不被称为_Layout.cshtml

我有一个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的调用和调用成功,所以我可以针点的问题脚本引用
  2. 脚本中提及的东西也都在正确的文件夹,即〜/脚本/ ....所以我知道这不是一个参考问题。
  3. 的脚本工作,我已经使用Web窗体项目
  4. 我试图引用捆绑在head部分_Layout.cshtml但没有改变
  5. 在VS铬和调试器开发工具测试显示他们的脚本没有被运行
  6. 我唯一的疑虑是有从_Layout.cshtml电话之间的某处差距,

我已经试过,其他人已经建议其他的解决方案在BundleConfig类束编辑在网络上,如寻找语法错误等......但据我所知,没有。

为了使脚本明显我已经包括了一个截屏结构: enter image description here

任何人都可以看到这个不同的角度比我,看看我在哪里呢?

+0

什么脚本没有运行?也许,这是脚本顺序问题? – Backs

+0

您是否在global.aspx中调用RegisterBundles? –

+0

在global.asax中调用了包。 StyleBundles被称为不是脚本包 – AnonDCX

回答

1

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

@using System.Web.Optimization 

这应该正确地执行Web.Optimization

唯一的其他部分可能是错误的,我们看不到是你的Global.asax。您需要确保您致电RegisterBundle

BundleConfig.RegisterBundles(BundleTable.Bundles); 
+0

我已经参考s.w.o和BundleConfig.RegisterBundle(BundleTable.Bundles);被称为global.asax – AnonDCX

+0

是的,但共享布局并不总是,它可以在ASP.net错误。 scripts.render的布局是否存在一个扭曲的问题? – Greg