2

I am trying to get add a css file to my layout file, which is in the Shared folder. When I run the web application, my CSS is not rendered (CSS that is not in the solution (ie: Bootstrap CDN) renders.

I have tried linking the CSS using these methods, and they don't work:

<link rel="stylesheet" href="@Url.Content("~/Assets/Scripts/StyleSheet.css")" type="text/css"/> 

<link rel="stylesheet" href="~/Assets/Scripts/StyleSheet.css" type="text/css"/> 

The File Structure For my app is:

enter image description here

Is there something I should be adding to the Startup.cs file so it can read css files?

Thanks.

EDIT: ERROR 404 WITH THE STYLE SHEET

I ran the application on Mozilla Firefox, and used the built in Developers Console to see whats happening. After observing the network tab, it tells me ERROR 404 Occurred while trying to retrieve the style sheet. This is really odd.

enter image description here

1
  • I checked the Developer Console on Mozilla Firefox, and saw Errror 404 occurred when trying to retrieve the style sheet (see image above). Commented Jan 23, 2016 at 16:52

2 Answers 2

1

Do other changes in your layout show up correctly? For instance if you add a random text string, does that show up? As far as startup.cs, you should have app.UseStaticFiles(); in the Configure section.

Once more note, my CSS in the _layout looks like this:

<environment names="Development">
    <link rel="stylesheet" href="~/css/site.css" />
</environment>
<environment names="Staging,Production">
    <link rel="stylesheet" href="~/css/site.min.css" asp-append-version="true" />
</environment>

I don't think it's required to be broken out, but I haven't tried it not broken out...

Sign up to request clarification or add additional context in comments.

3 Comments

Hi. Text on my layout page does appear on my other pages. Classes that I use from the bootstrap css work (the files are hosted on MaxCDN). I have checked my Startup.cs file, and in the Configure method, I do have app.UseStaticFiles().
First, check the generated html to see what it shows for the css url - it should not contain the "~". Take the url from the page and try and go to that in your browser using the relative path as specified in the css url. If all that looks good, use a browser tool such as firebug or chrome developer tools to determine what is being requested and how the server is responding.
I have checked this with the browser - It says the style sheets cannot be found. This is odd because I am sure the style sheet is in the correct location.
0

With Reference to a similar problem posted on StackOverflow: ASP.NET 5 MVC6 Custom CSS & Javascript placing convention

I placed the CSS File in the wwwroot directory, in a css folder.

enter image description here

Then, in my Layout file, I changed the href attribute:

<link rel="stylesheet" href="@Url.Content("../css/style.css")"/>

Finally, I made sure I had app.UseStaticFiles() in my Configure method:

public void Configure(IApplicationBuilder app)
    {
        app.UseStaticFiles();

        app.UseMvc(config => config.MapRoute(
            name: "Default",
            template: "{Controller}/{action}/{id?}",
            defaults: new {controller = "Home", action = "Index"}               
            ));
    }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.