简体   繁体   English

.NET-在布局顶部的剃刀视图中添加额外的样式表

[英].NET - Adding an extra style sheet to a razor view ontop of layout

I'm creating a new view and I want to add some custom CSS, so I created a new CSS sheet in the content folder called ClientDetails and referenced it at the top of the sheet as I read on another thread of adding specific CSS sheets to a view: 我正在创建一个新视图,并想添加一些自定义CSS,所以我在内容文件夹中创建了一个名为ClientDetails的新CSS表单,并在该表单的顶部引用了该表单,因为我在向其添加特定CSS表单的另一个线程上进行了阅读一个看法:

@model Linkofy.Models.Client

@{
ViewBag.Title = "Details";
}

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

<h2>@Html.DisplayFor(model => model.clientN)</h2>

However with the @section Styles bit I get this error: 但是,通过@section Styles位,我得到此错误:

System.Web.HttpException: The following sections have been defined but have not been rendered for the layout page "~/Views/Shared/_Layout.cshtml": "Styles". System.Web.HttpException:已定义以下部分,但尚未为布局页面“〜/ Views / Shared / _Layout.cshtml”:“样式”呈现。

Though if I remove the @section styles bit it works fine, I wondered if its only rendering that sheet and not the layout CSS if it's overriding it or something? 虽然如果我删除@section样式位,它可以正常工作,但我想知道它是否仅呈现该工作表而不是布局CSS(如果它覆盖了它或其他东西)? how do I add it on top of all the other sheets needed for the display. 如何将其添加到显示所需的所有其他表的顶部。

I have tried looking around for the answer to this though if it is a duplicate please link me to the question and I will close it, thanks! 我已经试着四处寻找答案,但是如果重复的话,请将我链接到该问题,我将结束它,谢谢!

Problem: 问题:

Server Error in '/' Application. “ /”应用程序中的服务器错误。 The following sections have been defined but have not been rendered for the layout page "~/Views/Shared/_Layout.cshtml": "Styles". 已经定义了以下部分,但尚未为布局页面“〜/ Views / Shared / _Layout.cshtml”:“样式”呈现。 Description: An unhandled exception occurred during the execution of the current web request. 说明:执行当前Web请求期间发生未处理的异常。 Please review the stack trace for more information about the error and where it originated in the code. 请查看堆栈跟踪,以获取有关错误及其在代码中起源的更多信息。 Exception Details: System.Web.HttpException: The following sections have been defined but have not been rendered for the layout page "~/Views/Shared/_Layout.cshtml": "Styles". 异常详细信息:System.Web.HttpException:已定义以下部分,但尚未为布局页面“〜/ Views / Shared / _Layout.cshtml”:“样式”呈现。

Solution: 解:

In your ~/Views/Shared/_Layout.cshtml render the section using 在〜/ Views / Shared / _Layout.cshtml中,使用

    @RenderSection("Styles", required: false)

See https://msdn.microsoft.com/en-us/library/gg537886(v=vs.111).aspx for more details. 有关更多详细信息,请参见https://msdn.microsoft.com/zh-cn/library/gg537886(v=vs.111).aspx

Also you must declare the layout: 另外,您必须声明布局:

@{
    Layout = "~/Views/Shared/_Layout.cshtml";
    ViewBag.Title = "Details";
}

The reason you are getting this error is explained in the message. 消息中说明了出现此错误的原因。 You probably have something like this in your layout page: 您的版面配置页面中可能有以下内容:

@RenderSection("Styles")

When you use it like that, it's required by default. 这样使用时,默认情况下是必需的。 Every page will be required to have the additional style section. 每个页面都必须具有附加的style部分。 Otherwise, you get the error you are seeing now. 否则,您将得到现在看到的错误。 What you need to do is make that section not required: 您需要做的是使该部分不是必需的:

@RenderSection("Styles", required: false)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM