MVC BundleConfig Css / Scripts未加载

时间:2015-03-10 16:22:16

标签: c# asp.net-mvc

我已经阅读了几篇文章并在互联网上试图找出为什么我的BundleConfig即使在本地运行也不能正常工作。我已经指定了脚本和css:

       bundles.Add(new StyleBundle("~/Content/css").Include(
            "~/Content/font-awesome.css",
            "~/Content/frontend.css",
            "~/Content/brand.css",
            "~/Content/style.css",
            "~/Content/variables.css",
            "~/Content/rateit.css",
            "~/Content/bootstrap.css",
            "~/Content/bootstrap.css"
            ));


        bundles.Add(new StyleBundle("~/Content/fonts").Include(
            "~/Fonts/Enzo/stylesheet.css",
            "~/Fonts/DinCondensed/stylesheet.css"
            ));

        bundles.Add(new ScriptBundle("~/bundles/jquery").Include(
            "~/Scripts/jquery-{version}.js",
            "~/Scripts/jquery.rateit.js"
            ));

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

        bundles.Add(new ScriptBundle("~/bundles/scripts").Include(
            "~/Scripts/main.js",
            "~/Scripts/mem-geninfo.js",
            "~/Scripts/fastclick.js"
            ));

        bundles.IgnoreList.Clear();

我已经在_Layout.cshtml中适当地引用了它们。

<head>
//Code removed for clarity
@Styles.Render("~/Content/css")
@Styles.Render("~/Content/fonts")
</head>

<body>
//Code Removed for clarity
@Scripts.Render("~/bundles/jquery")
@Scripts.Render("~/bundles/bootstrap")
@Scripts.Render("~/bundles/scripts")
@RenderSection("scripts", required: false)
</body>

我的web.config:

<compilation debug="true" targetFramework="4.5" />

但是一切都在页面加载时生成 404 Not Found 。使用Chrome进行调试时,内容捆绑文件夹会显示在我的localhost网站下。我有其他项目,它的工作正常。我继承了这个项目,我认为它刚刚开始作为一个空白的MVC项目。因此,配置中可能缺少某些东西。因此,无法让Bundles工作。

更新 - Web.config:

<configuration>
  <appSettings>
    <add key="webpages:Version" value="3.0.0.0" />
    <add key="webpages:Enabled" value="false" />
    <add key="PreserveLoginUrl" value="true" />
    <add key="ClientValidationEnabled" value="true" />
    <add key="UnobtrusiveJavaScriptEnabled" value="true" />
  </appSettings>
  <system.web>
    <compilation debug="true" targetFramework="4.5" />
    <httpRuntime targetFramework="4.5" />
    <pages>
      <namespaces>
        <add namespace="System.Web.Helpers" />
        <add namespace="System.Web.Mvc" />
        <add namespace="System.Web.Mvc.Ajax" />
        <add namespace="System.Web.Mvc.Html" />
        <add namespace="System.Web.Routing" />
        <add namespace="System.Web.WebPages" />
      </namespaces>     
    </pages>
  </system.web>
  <system.webServer>
    <validation validateIntegratedModeConfiguration="false" />
    <handlers>
      <remove name="ExtensionlessUrlHandler-Integrated-4.0" />
      <remove name="OPTIONSVerbHandler" />
      <remove name="TRACEVerbHandler" />
      <add name="ExtensionlessUrlHandler-Integrated-4.0" path="*." verb="*" type="System.Web.Handlers.TransferRequestHandler" preCondition="integratedMode,runtimeVersionv4.0" />
    </handlers>
    <modules runAllManagedModulesForAllRequests="true">
      <remove name="BundleModule" />
      <add name="BundleModule" type="System.Web.Optimization.BundleModule" />
    </modules>
  </system.webServer>
  <runtime>
    <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
      <dependentAssembly>
        <assemblyIdentity name="System.Web.Helpers" publicKeyToken="31bf3856ad364e35" />
        <bindingRedirect oldVersion="1.0.0.0-3.0.0.0" newVersion="3.0.0.0" />
      </dependentAssembly>
      <dependentAssembly>
        <assemblyIdentity name="System.Web.WebPages" publicKeyToken="31bf3856ad364e35" />
        <bindingRedirect oldVersion="1.0.0.0-3.0.0.0" newVersion="3.0.0.0" />
      </dependentAssembly>
      <dependentAssembly>
        <assemblyIdentity name="System.Web.Mvc" publicKeyToken="31bf3856ad364e35" />
        <bindingRedirect oldVersion="1.0.0.0-5.2.3.0" newVersion="5.2.3.0" />
      </dependentAssembly>
      <dependentAssembly>
        <assemblyIdentity name="WebGrease" publicKeyToken="31bf3856ad364e35" culture="neutral" />
        <bindingRedirect oldVersion="0.0.0.0-1.5.2.14234" newVersion="1.5.2.14234" />
      </dependentAssembly>
    </assemblyBinding>
  </runtime>
</configuration>

关于我做错的任何建议?

由于

2 个答案:

答案 0 :(得分:4)

尝试在system.webServer部分

中将此添加到您的web.config中
<modules runAllManagedModulesForAllRequests="true">  
  <remove name="BundleModule" />  
  <add name="BundleModule" type="System.Web.Optimization.BundleModule" />  
</modules>  

修改
既然你说你继承了这个项目,那么确保你注册的是bundle,通常是在Global.asax中 -

protected void Application_Start()
{
    ...
    BundleConfig.RegisterBundles(BundleTable.Bundles);
}

很容易忽视这个

答案 1 :(得分:0)

该帖子有时会发布,但它可能会帮助某人寻找它,我发现,如果您在发布时将捆绑包名称定义为文件路径,则不管您在开发人员机器上是否看到任何名称,该名称都可以使用到IIS,捆绑程序找不到您的样式和脚本

假设您有此文件

~/Content/plugins/summernote/summernote-s4.css
~/Content/plugins/summernote/summernote.css

将包命名为路径,并在末尾加上一个额外的单词,以这种方式称呼“包”

bundles.Add(new StyleBundle("~/Content/plugins/summernote/bundles").Include(
   "~/Content/plugins/summernote/summernote-bs4.css",
   "~/Content/plugins/summernote/summernote.css"));

希望有帮助