ChatGPT解决这个技术问题 Extra ChatGPT

How to reference a .css file on a razor view?

I know how to set .css files on the _Layout.cshtml file, but what about applying a stylesheet on a per-view basis?

My thinking here is that, in _Layout.cshtml, you have <head> tags to work with, but not so in one of your non-layout views. Where do the <link> tags go?


M
MrBoJangles

For CSS that are reused among the entire site I define them in the <head> section of the _Layout:

<head>
    <link href="@Url.Content("~/Styles/main.css")" rel="stylesheet" type="text/css" />
    @RenderSection("Styles", false)
</head>

and if I need some view specific styles I define the Styles section in each view:

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

Edit: It's useful to know that the second parameter in @RenderSection, false, means that the section is not required on a view that uses this master page, and the view engine will blissfully ignore the fact that there is no "Styles" section defined in your view. If true, the view won't render and an error will be thrown unless the "Styles" section has been defined.


You know, on second thought, that's not so bad. I think it's just new and different.
@section Styles --> says cannot resolve section Styles, what does it mean?
@Sam, it means that there's no such section defined in your Layout.
@DarinDimitrov Is there a way to render at the exact position instead of at the end of the header. I would like to keep a specific order for the css priorities.
@Marc It's rendered on a place, where you call RenderSection (surprisingly :), not at the end of the header.
M
MrBoJangles

I tried adding a block like so:

@section styles{
    <link rel="Stylesheet" href="@Href("~/Content/MyStyles.css")" />
}

And a corresponding block in the _Layout.cshtml file:

<head>
<title>@ViewBag.Title</title>
@RenderSection("styles", false);
</head>

Which works! But I can't help but think there's a better way. UPDATE: Added "false" in the @RenderSection statement so your view won't 'splode when you neglect to add a @section called head.


There isn't a better way, although I would name the section "Head".
You're exactly right. Calling it "pageStyle" suggests that it's only for that purpose.
If you do it like this, you would need to add "MyStyles" in all Views, I would go with Darins answer.
Right-o. That's why I added the false argument to @RenderSection(). Good catch.
C
Community

Using

@Scripts.Render("~/scripts/myScript.js")

or

@Styles.Render("~/styles/myStylesheet.css")

could work for you.

https://stackoverflow.com/a/36157950/2924015


Please do not post links to duplicate answers. Instead, consider other actions that could help future users find the answer they need, as described in the linked post.
s
shanethehat

layout works the same as an master page. any css reference that layout has, any child pages will have.

Scott Gu has an excellent explanation here


Much obliged. However, my question is, how do I set a reference to a single view, not the "master".
S
Suraj Rao

I prefer to use the razor html helper from Client Dependency dll

Html.RequireCss("yourfile", 9999); // 9999 is loading priority 

S
SwissCodeMen

You can this structure in _Layout.cshtml file

<link href="~/YourCssFolder/YourCssStyle.css" rel="stylesheet" type="text/css" />

How does this allow me to selectively apply a style sheet per view?
You can also add a class or id to each section, and inside the header you could have a styles like the one mentioned by mofidul. What I do is I work with sass, so each view has a separate class container. In that way I created sass pages for each section, which in the end is more structured and organized.
what is the meaning of rel="stylesheet"?
@SvenKrauter <link> can also be used for things other than stylesheets (ie, prefetching next pages, favicons), so rel="stylesheet" tells the browser that the referenced file is a stylesheet.