asp.net - 将 .net 项目转换为使用布局后页面顶部出现空白

标签 asp.net asp.net-mvc asp.net-mvc-4 asp.net-mvc-3 razor

我最近将我的 .net 项目更改为使用母版页/布局页。现在,当我的所有页面都呈现时,页面顶部导航栏之前有一些空间。

enter image description here

我在 Chrome 中检查了该页面,正文顶部有一些空引号,在检查器中删除这些引号可以解决问题。但我无法理解它们来自哪里?这是我的索引的页面结构。

@{
  Layout = "~/Views/Shared/_Layout.cshtml";
  ViewBag.Title = "my title";
}
<!--HERO IMAGE-->
<div>
    //index page's code comes here
</div>

@section additionalStyles{
   //adding external stylesheets
}

我在索引页中使用渲染部分来加载一些在其余页面中没有用的样式表。这些在布局页面的结束标记之前加载,如下所示:

<!DOCTYPE html>
<html lang="en" class="no-js">
<head>
  <meta charset="utf-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">

  <title>@ViewBag.Title</title>

  <!-- All STYLESHEETS ARE ADDED AT THIS PLACE-->

  @RenderSection("additionalStyles", required: false);
</head>
<body>
  <div class="navbar">
       //navbar's code  comes here
  </div>

    @RenderBody();

  <!-- FOOTER -->

  @RenderSection("additionalScripts", required: false);

  <!-- ALL SCRIPTS ARE ADDED HERE-->
</body>
</html>

什么可能导致引用?

P.s:我刚刚注意到我的许多页面都有间距和引号,因此它们可能来自布局文件,或者可能这些部分导致了问题,但我不完全知道如何解决它

编辑:实际上定义了布局页面要呈现的两个部分的第二个页面实际上获取了其中的三个“”“引号”“”。

  • 在第一个 renderSection(additionalStyles) 的末尾
  • 在 renderBody() 放置的内容末尾有一个
  • 在 renderSection(additionalScripts) 放置的内容末尾添加一个

这是某种模式吗?

最佳答案

如果你仔细观察,它会插入分号;

@RenderSection("additionalStyles", required: false);

@RenderBody();

您的代码在 @RenderSection 之后有不必要的分号,并且正在生成它们以供显示。删除它们,“空间”就会消失。

请参阅 https://learn.microsoft.com/en-us/aspnet/web-pages/overview/getting-started/introducing-razor-syntax-c 的第 3 节用于分号的使用。

  1. Inside a block, you end each code statement with a semicolon

    Inside a code block, each complete code statement must end with a semicolon. Inline expressions don't end with a semicolon.

<!-- Single-statement block -->
@{ var theMonth = DateTime.Now.Month; }

<!-- Multi-statement block -->
@{
    var outsideTemp = 79;
    var weatherMessage = "Hello, it is " + outsideTemp + " degrees.";
}

<!-- Inline expression, so no semicolon -->
<p>Today's weather: @weatherMessage</p>

关于asp.net - 将 .net 项目转换为使用布局后页面顶部出现空白,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43075262/

相关文章:

asp.net-mvc - MVC 5 外部身份验证,身份验证模式=表单

.net - 将.NET类用作带有SSRS RDLC的数据源

c# - 无法获取 RowUpdate 事件处理程序以显示 GridView 的错误

c# - 格式化逻辑属于 MVC 的什么地方?

asp.net - 使用 ASP.net 的谷歌 HTML 快照

c# - 强类型通用属性替代方案

c# - ModelState.isvalid 返回 false?

c# - MVC 路由去掉 URL 中的/Index

c# - 使用 Identity 将表添加到 Code-First ASP.NET 应用程序

c# - 如何使用 serilog 通过身份验证在 kibana 中将日志设置为 ELK