除了 _Layout.cshtml 中已链接的内容之外,我还想在某些视图中链接特定的样式表。对于非 Razor,我看到使用内容占位符。我该如何为 Razor 执行此操作?
Razor 中的内容占位符相当于部分。
在你的 _Layout.cshtml 中:
<head>
@RenderSection("Styles", required: false)
</head>
然后在您的内容页面中:
@section Styles {
<link href="@Url.Content("~/Content/StandardSize.css")" />
}
另一种解决方案是将样式放入 ViewBag/ViewData 中:
在你的 _Layout.cshtml 中:
<head>
@foreach(string style in ViewBag.Styles ?? new string[0]) {
<link href="@Url.Content(style)" />
}
</head>
在您的内容页面中:
@{
ViewBag.Styles = new[] { "~/Content/StandardSize.css" };
}
这是有效的,因为视图页面在布局之前执行。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)