ASP.NET MVC - Styles and Layout

To learn ASP.NET MVC, we will build an Internet application.

Part 3:Add styles and a unified appearance (layout).

Add layout

File _Layout.cshtml represents the layout of each page in the application. It is located in the Shared folder within the Views folder.

Open this file and replace its content with:

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title>@ViewBag.Title</title>
<link href="@Url.Content("~/Content/Site.css")" rel="stylesheet" type="text/css" />
<script src="@Url.Content("~/Scripts/jquery-1.5.1.min.js")"></script>
<script src="@Url.Content("~/Scripts/modernizr-1.7.min.js")"></script>
</head>
<body>
<ul id="menu">
<li>@Html.ActionLink("Home", "Index", "Home")</li>
<li>@Html.ActionLink("Movies", "Index", "Movies")</li>
<li>@Html.ActionLink("Over", "About", "Home")</li>
</ul> 
<section id="main">
@RenderBody()
<p>Copyright W3school 2012. Alle Rechten Voorbehouden.</p>
</section>
</body>
</html>

HTML-hulpmiddelen

In de bovenstaande code worden HTML-hulpmiddelen gebruikt om de HTML-uitvoer te wijzigen:

@Url.Content() - URL-inhoud hier invoegen.

@Html.ActionLink() - HTML-koppeling hier invoegen.

We zullen later in dit handboek HTML-hulpmiddelen uitleggen.

Razor Syntax

In de bovenstaande code is de code die met de rode markering is gemaakt met Razor-markeringen in C#.

@ViewBag.Title - Voeg hier de paginatitel in.

@RenderBody() - Hier wordt de inhoud van de pagina weergegeven.

Je kunt dit in onze Razor-tutorialLeer C# en VB (Visual Basic) geschreven Razor-markeringen.

Voeg stijl toe

De stijltafel van de applicatie is Site.css. Het bevindt zich in de map Content.

Open het bestand Site.css en vervang de inhoud ervan met:

lichaam
{
lettertype: "Trebuchet MS", Verdana, sans-serif;
achtergrondkleur: #5c87b2;
kleur: #696969;
}
h1
{
onderstreep: 3px vast #cc9900;
lettertype: Georgia, serif;
kleur: #996600;
}
#main
{
padding: 20px;
background-color: #ffffff;
hoekradius: 0 4px 4px 4px;
}
a
{
kleur: #034af3; 
}
/* Menu-stijl ------------------------------*/
ul#menu
{
padding: 0px;
positie: relatief;
marge: 0;
}
ul#menu li
{
display: inline;
}
ul#menu li a 
{
achtergrondkleur: #e8eef4;
padding: 10px 20px;
text-decoration: none;
line-height: 2.8em;
/*CSS3 properties*/
border-radius: 4px 4px 0 0;
}
ul#menu li a:hover
{
background-color: #ffffff;
} 
/* 表单样式 ------------------------------*/
fieldset
{
padding-left: 12px;
} 
fieldset label
{
display: block;
padding: 4px;
}
input[type="text"], input[type="password"]
{
width: 300px;
}
input[type="submit"]
{
padding: 4px;
}
/* 数据样式 ------------------------------*/
table.data
{
background-color:#ffffff;
border:1px solid #c3c3c3;
border-collapse:collapse;
width:100%;
}
table.data th
{
background-color:#e8eef4;
border:1px solid #c3c3c3;
padding:3px;
}
table.data td 
{
border:1px solid #c3c3c3;
padding:3px;
}

_ViewStart 文件

Shared 文件夹(位于 Views 文件夹内)中的 _ViewStart 文件包含以下内容:

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

这段代码被自动添加到由应用程序显示的所有视图。

如果删除该文件,则必须向所有视图添加这段代码。

您将在本教程稍后的章节学到更多有关视图的知识。