ASP.NET MVC - Visninger

For at lære ASP.NET MVC vil vi bygge en Internet-applikation.

Del 5:Tilføj visninger til at vise applikationen.

Views Mappe

Views Mappene gemmer filer, der er relateret til applikationens visning (brugergrænseflade) (HTML-filer). Afhængigt af sprog kan disse filers udvidelser være html, asp, aspx, cshtml og vbhtml.

Views Mappe indeholder en Mappe for hver controller.

Visual Web Developer har oprettet en Mappe Account, en Mappe Home og en Mappe Shared (i Views Mappe).

Account 文件夹包含用于注册并登录用户帐户的页面。

Home 文件夹用于存储诸如首页和关于页之类的应用程序页面。

Shared 文件夹用于存储控制器间分享的视图(模板页和布局页)。

ASP.NET 文件类型

能够在 Views 文件夹中找到以下 HTML 文件类型:

文件类型 扩展名
纯 HTML .htm or .html
经典 ASP .asp
经典 ASP.NET .aspx
ASP.NET Razor C# .cshtml
ASP.NET Razor VB .vbhtml

Index 文件

文件 Index.cshtml 表示应用程序的首页。它是应用程序的默认文件(首页文件)。

在文件中写入以下内容:

@{ViewBag.Title = "Home Page";}
<h1>Welcome to CodeW3C.com</h1>
<p>Put Home Page content here</p>

About 文件

文件 About.cshtml 表示应用程序的关于页面。

在文件中写入以下内容:

@{ViewBag.Title = "About Us";}
<h1>About Us</h1>
<p>Put About Us content here</p>

运行应用程序

选择调试,从调试菜单中启动调试(或者按 F5)。

您的应用程序将类似这样:

请点击“首页”和“关于”,看看它是如何运行的。

祝贺你

祝贺您。您已经创建好了第一个 MVC 应用程序。

注释:“电影”选项卡仍然无法点击。我们将在本教程稍后的章节中为“电影”选项卡添加代码。