【问题标题】:MVC BundleConfig Css/Scripts Not LoadingMVC BundleConfig Css/脚本未加载
【发布时间】:2015-05-12 04:45:30
【问题描述】:

我已经阅读了几篇文章并在整个互联网上试图找出为什么我的 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 进行调试时,Content 和 bundles 文件夹会显示在我的本地主机站点下。我还有其他项目,它在它们中运行良好。我继承了这个项目,我认为它只是作为一个空白 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>

关于我做错了什么有什么建议吗?

谢谢

【问题讨论】:

  • 你建好项目了吗?
  • 当然。我整个上午都在处理它。

标签: c# asp.net-mvc


【解决方案1】:

尝试将其添加到您的 web.config 中的 system.webServer 部分

<modules runAllManagedModulesForAllRequests="true">  
  <remove name="BundleModule" />  
  <add name="BundleModule" type="System.Web.Optimization.BundleModule" />  
</modules>  

编辑
既然您说您要继承项目,请确保您正在注册捆绑包,通常在 Global.asax -

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

很容易忽略这一点

【讨论】:

  • 您可以尝试发布更多您的 web.config 吗?观察。删除敏感字符串等...
  • 该死的。我是个白痴。我有没有提到我继承了这个项目?我完全忽略了这一点。谢谢。
  • runAllManagedModulesForAllRequests="true" 作为捆绑工作的尝试太过分了。
  • 如果您创建了一个空白项目并添加了 Microsoft ASP.NET Web 优化框架,则编辑 Global.asax 尤其重要。对于 BundleConfig,您还需要为 BundleTable 添加对 System.Web.Optimization 的引用。
【解决方案2】:

这篇文章有一段时间,但它可能会帮助寻找这个的人,我发现如果你将你的包名称定义为你的文件路径,它可以工作,无论你是否在你的开发者机器上看到任何名称,当你发布时到 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"));

希望对你有帮助

【讨论】:

    猜你喜欢
    • 2020-11-08
    • 1970-01-01
    • 1970-01-01
    • 2017-12-12
    • 2017-03-08
    • 2016-10-02
    • 1970-01-01
    • 1970-01-01
    • 2021-10-12
    相关资源
    最近更新 更多