Do not load the scripts from the BundleConfig.cs

0

I am starting a project in ASP .NET MVC 5 in Visual Studio 2017, I have created my BundleConfig.cs file with the following information:

using System.Web;
using System.Web.Optimization;
namespace Login
{
    public class BundleConfig
    {
        public static void RegisterBundles(BundleCollection bundles)
        {
            // jQuery
            bundles.Add(new ScriptBundle("~/bundles/jquery").Include(
                        "~/Scripts/jquery-3.1.1.min.js"));
            // CSS
            bundles.Add(new StyleBundle("~/Content/css").Include(
                      "~/Content/bootstrap.min.css",
                      "~/Content/animate.css",
                      "~/Content/style.css"));
        }
    }
}

I have in my _LayoutPage.cshtml the following in the header:

<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    @Styles.Render("~/Content/css")
    @Styles.Render("~/font-awesome/css")
</head>

and when loading my page it appears without styles, if I check the console (F12) of the browser I see the routes that were not found with an error 404

    
asked by JuankGlezz 19.04.2017 в 20:10
source

3 answers

1

Appears without styles because the configuration of the Bundles was not read, in the file Global.asax.cs it is necessary to add:

BundleConfig.RegisterBundles(BundleTable.Bundles);

leaving the file in this way:

using System.Web.Mvc;
using System.Web.Optimization;
using System.Web.Routing;
namespace Login
{
    public class MvcApplication : System.Web.HttpApplication
    {
        protected void Application_Start()
        {
            AreaRegistration.RegisterAllAreas();
            RouteConfig.RegisterRoutes(RouteTable.Routes);
            BundleConfig.RegisterBundles(BundleTable.Bundles);
        }
    }
}
  

NOTE: Keep in mind that you have to add the reference of System.Web.Optimization;

On the other hand you also have to verify that in the web.config of the views there is namespaces the name of System.Web.Optimization otherwise you have to add it as follows:

<namespaces>
  <add namespace="System.Web.Mvc" />
  <add namespace="System.Web.Mvc.Ajax" />
  <add namespace="System.Web.Mvc.Html" />
  <add namespace="System.Web.Optimization" />
  <add namespace="System.Web.Routing" />
  <add namespace="Login" />
</namespaces>
    
answered by 19.04.2017 / 20:10
source
1

In the following ~/Content/cssdesign is my content to be loaded with the customized bundle (let's say) under that label. You can also add it to existing ones and not have to create a new one.

Do the following; on the corresponding Layout page the @Styles.Render("~/Content/cssdesign") ; and in BundleConfigs.cs

    bundles.Add(new StyleBundle("~/Content/cssdesign").Include(
              /* otras referencias CSS */
              "~/Content/font-awesome.css"));

Adding it as shown below:

// Omito lo anterior

    public static void RegisterBundles(BundleCollection bundles)
    {
        // jQuery
        bundles.Add(new ScriptBundle("~/bundles/jquery").Include(
                    "~/Scripts/jquery-3.1.1.min.js"));
        // CSS
        bundles.Add(new StyleBundle("~/Content/css").Include(
                  "~/Content/bootstrap.min.css",
                  "~/Content/animate.css",
                  "~/Content/style.css"));

        bundles.Add(new StyleBundle("~/Content/cssdesign").Include(
                  /* otras referencias CSS */
                  "~/Content/font-awesome.css"));
    }
// Omito lo que posterior

Note: If it's CSS in BundleConfig use StyleBundle , if it's JavaScript ScriptBundle ; if you mistakenly use the wrong way it will mark errors when loading the page. I have not moved anymore and it works.

    
answered by 19.04.2017 в 20:29
0

Verify that you configured a bundle for '~ / font-awesome / css'.

The ScriptBundle constructor parameter indicates what your bundle will be called, and is used to refer to it.

bundles.Add(new ScriptBundle("~/[nombre_del_bundle]").Include(
            "[archivos_que_contiene_el_bundle1]"));

And you mean the like:

@Scripts.Render("~/[nombre_del_bundle]")
    
answered by 19.04.2017 в 23:51