3

I have defined this bit of code in the <head> section of my _Layout page.

@RenderSection("Styles", false)

Then my index page

@section Styles {
<link rel="stylesheet" type="text/css" href="@Url.Content("~Views/zCSS/ManualProposalWindow.css")" />    
}

My Index page is in Views/Home/Index

My CSS is in Views/CSS/ManualProposalWindow.css

I know the styles are correct because if I put them in a style block inside the index it works fine.

This is the CSS I'm trying to load:

body {
       background: black;
}
2
  • Troubleshooting questions: 1) Load the page. Is the link element in the markup? 2) Using Firebug's network tab, is the stylesheet resource downloading correctly? Commented Mar 11, 2014 at 14:32
  • Can StyleSheets only be referenced in the Content folder?? because when I put in the content folder it worked fine Commented Mar 11, 2014 at 14:39

3 Answers 3

5

Files within the views folder cannot be loaded. They will throw a 404 error due to the httpHandler configured in the Web.config there. This is for security, as you do not want your .cshtml files publicly accessible.

<httpHandlers>
  <add path="*" verb="*" type="System.Web.HttpNotFoundHandler"/>
</httpHandlers>

Include the css file from any other folder and you should be fine.

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

1 Comment

thanks for the knowledge, I thought it was something obscure like this after it worked when I switched locations
5

Your index page should be like this:

@section Styles
{
    @Styles.Render("~/Content/styleFile.css")
    @Styles.Render("~/Content/orStyleBundle")
}

Also, put your styles in the Content (or similar) folder in your solution.

1 Comment

I get an error: The name 'Styles' does not exist in current context. Perhaps I'm on a different version of C#?
0
@section Styles {
            <style>
                .table-striped tbody > tr:nth-child(odd) > td, .table-striped tbody > tr:nth-child(odd) > th {
                    background-color: none;
                }
        
                .table-striped tbody > tr:nth-child(odd) > td, .table-striped tbody > tr:nth-child(odd) > th {
                    background-color: none;
                }
            </style>   
        }

1 Comment

As it’s currently written, your answer is unclear. Please edit to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers in the help center.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.