MVC 4 - Script/Css Rendering Problems

asp.net-mvc, c#, css, jquery, razor

Solution

`~/Content/css/` and `~/Scripts/` aren't file paths. They are bundle names, so if you don't have a Bundle file follow these steps:

In your `App_Start` folder add a `BundleConfig` Class:

 public class BundleConfig
 {

 }

Add a `RegisterBundles` method like this:

public static void RegisterBundles(BundleCollection bundles)
{

}

Then you can add a bundle, write this code into your method:

 bundles.Add(new StyleBundle("~/Content/css").Include("~/Content/site.css"));

Here `~/Content/css` is your Bundle Name and `~/Content/site.css` is your Css File Path.Change it if your css file in a different path.

Now there is one more step, open your `Global.asax` file and add this code into your `Application_Start` :

BundleConfig.RegisterBundles(BundleTable.Bundles);

Then you can render your Bundle with:

@Styles.Render("~/Content/css")

And also you can render more than one file with one bundle, to do this add your all file paths here like this:

bundles.Add(new StyleBundle("~/Content/css")
.Include("~/Content/site.css",
         "~/Content/sample.css",
         "~/Content/sample2.css"));

PS: Also you can take a look at this question about benefits of using Bundles: Usage of the ASP.NET MVC4 jquery/javascript bundles

Problem

I get the following error mesasges in my browser console: GET http://*:54559/Scripts/ 403 (Forbidden) http://:54559/Content/css/ 400 (Bad Request) Uncaught ReferenceError: $ is not defined In my _Layout.shtml I use the following code to try and include css/bootstrap. ``` @Styles.Render("~/Content/css/*") <!-- JavaScript --> @Scripts.Render("~/Scripts/") @RenderSection("scripts", required: false) <script> // Activates the Carousel $('.carousel').carousel({ interval: 5000 }) </script> ``` The file paths look ok as in my project the css is located at "Content/css" and scripts is just a folder at the top level called "Scripts" Any ideas?

Original source

Related problems