如何在剃刀视图上引用 .css 文件? [英] How to reference a .css file on a razor view?

查看:16
本文介绍了如何在剃刀视图上引用 .css 文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道如何在 _Layout.cshtml 文件上设置 .css 文件,但如何在每个视图的基础上应用样式表?

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

我的想法是,在 _Layout.cshtml 中,您可以使用 <head> 标签,但在您的非布局视图之一中则不然. 标签在哪里?

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?

推荐答案

对于在整个站点中重复使用的 CSS,我在 _Layout<的 部分定义了它们/代码>:

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>

如果我需要一些特定于视图的样式,我会在每个视图中定义 Styles 部分:

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" />
}

知道@RenderSection 中的第二个参数 false 表示在使用此母版页的视图上不需要该部分,并且视图引擎将幸福地忽略没有样式"的事实,这一点很有用" 在您的视图中定义的部分.如果为 true,除非定义了样式"部分,否则视图将不会呈现并抛出错误.

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.

这篇关于如何在剃刀视图上引用 .css 文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆