简体   繁体   中英

MVC BundleConfig Css/Scripts Not Loading

I've read several articles and been all over the internet trying to figure out why my BundleConfig isn't working properly even when running locally. I've got the scripts and css specified:

       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();

And I've referenced them appropriately in the _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>

My web.config:

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

But everything is generating 404 Not Found on page load. And when debugging with Chrome the and folders show up under my localhost site.文件夹会显示在我的本地主机站点下。 I've got other projects and it works just fine in them. I inherited this project and I think it was just started as a Blank MVC Project. So possibly something is missing from configs. Therefore can't get the Bundles to work at all.

UPDATE - 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>

Any advice on what I'm doing wrong?

Thanks

Try adding this to your web.config, in the system.webServer section

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

EDIT
Since you said you're inheriting the project, ensure you are registering the bundles, usually in Global.asax -

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

It's easy to overlook this

The post have sometime but it might help someone looking for this, I have found that if you define your bundle name as your files path, it works, no matter if you see any name working on your developer machine, when you publish to IIS your styles and scripts are not finded by the bundler

Let's say you have this files

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

Name your bundle as the path with an extra word at the end, let's say 'bundles' in such a way it gets this way

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

Hope it helps

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM