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

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

问题描述

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

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> 标签一起工作,但没有那么在您的非布局视图之一。哪里做<链路GT; 标签去

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,这是在整个网站中重复使用我在&LT定义它们在 _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>

如果我需要一些具体的看法我的风格定义了样式在每个视图部分:

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

编辑:这是非常有用的知道,在@RenderSection第二个参数,假的,意味着该部分未在使用该母版页视图,且该视图引擎会幸福地忽略了一个事实,有没有风格在视图定义部分。如果为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天全站免登陆