7

因此我有一个布局页面

<!DOCTYPE html>
<html>
<head>
    <title>@ViewBag.Title</title>
    <script src="@Url.Content("~/Scripts/jquery-1.7.1.min.js")" type="text/javascript"></script>
    <link href="@Url.Content("~/content/main_layout.css")" rel="stylesheet" type="text/css" />
    <link href="@Url.Content("~/content/menu.css")" rel="stylesheet" type="text/css" />
    @RenderSection("JS", required:false)
    @RenderSection("CSS", required:false)
</head>
<body>
    @RenderBody()
</body>
</html>

然后是这样的视图

@{
    ViewBag.Title = "Home";
    Layout = "~/views/shared/_layout.cshtml";
}
@using RS.Common.HtmlHelpers;
@section JS
{
    <script src="@Url.Content("~/scripts/fue.js")" type="text/javascript"></script>
    <script src="@Url.Content("~/scripts/jquery.flexslider-min.js")" type="text/javascript"></script>
}
@section CSS 
{
    <link href="@Url.Content("~/content/hp.css")" rel="stylesheet" type="text/css" />
    <link href="@Url.Content("~/content/hp_form.css")" rel="stylesheet" type="text/css" />
    <link href="@Url.Content("~/content/flexslider.css")" rel="stylesheet" type="text/css" />
}

<h4>Test!</h4>

页面加载正常,如我所料。但是,我收到 3 个警告:

Validation (XHTML 1.0 Transitional): Element 'link' cannot be nested within element 'link'

此错误针对视图上 CSS 部分中的每个标记。

在这个阶段这只是一个烦恼,但我想知道这个错误的原因(以及解决方案)可能是什么?

4

1 回答 1

9

您已指定 HTML5 DOCTYPE 并尝试验证为XHTML 1.0 Transitional. 我想最终呈现的 HTML 如下所示:

<!DOCTYPE html>
<html>
<head>
    <title>Home</title>
    <script src="/Scripts/jquery-1.7.1.min.js" type="text/javascript"></script>
    <link href="/content/main_layout.css" rel="stylesheet" type="text/css" />
    <link href="/content/menu.css" rel="stylesheet" type="text/css" />

    <script src="/scripts/fue.js" type="text/javascript"></script>
    <script src="/scripts/jquery.flexslider-min.js" type="text/javascript"></script>

    <link href="/content/hp.css" rel="stylesheet" type="text/css" />
    <link href="/content/hp_form.css" rel="stylesheet" type="text/css" />
    <link href="/content/flexslider.css" rel="stylesheet" type="text/css" />
</head>
<body>
    <h4>Test!</h4>
</body>
</html>

您可能希望RenderSection在布局中颠倒两者的顺序,以便将链接和脚本组合在一起。或者您也可以考虑将脚本声明移到页面末尾:

像这样:

<!DOCTYPE html>
<html>
<head>
    <title>@ViewBag.Title</title>
    <link href="@Url.Content("~/content/main_layout.css")" rel="stylesheet" type="text/css" />
    <link href="@Url.Content("~/content/menu.css")" rel="stylesheet" type="text/css" />
    @RenderSection("CSS", required:false)
</head>
<body>
    @RenderBody()

    <script src="@Url.Content("~/Scripts/jquery-1.7.1.min.js")" type="text/javascript"></script>
    @RenderSection("JS", required:false)
</body>
</html>
于 2012-05-03T15:10:51.283 回答