ASP.NET CORE MVC LE VIEWS

CREATE THE FIRST CONTENT VIEW

NET CORE

Razor is the name of the view-engine, which is that component within .NET Core MVC applications that is responsible for processing views to produce HTML.

The views are composed of HTML code mixed with C# code, hence the .cshtml extension. Let’s look at where views should be created and the subdivision of directories.

Prima view
Struttura directory

The name of the View must be the same as that of the Action within the Controller. Let’s edit the Controller and display the result.

CoursesController
Browser

CREATE THE LAYOUT VIEW

All the outline elements header, footer, body then the HTML elements of the document itself, are common elements that affect all views, so we should move them out of the content pages and put them into Layout Views so that they can be reused easily in each Content View.

Parti duplicate

The figure shows how views are created by combining the Layout View and the Content View.

Layout

The instruction @RenderBody() tells the Razor engine how the Layout View containing the fixed parts common to each View and the Content View, with specific markup for each View to form the complete View, are to be combined. The instruction @RenderSection(“scripts”, false) indicates whether some sections should be present in the Content Views via the boolean parameter.

Body e section

We have eliminated the repeated code, however there are still some parts, for example in the Layout statement that is repeated on the three content views. We can remedy this as well by introducing the View _ViewStart.cshtml. Inside this View we will put all the instructions common to the content views.

_ViewStart

LINK TO CODE ON GITHUB

GITHUB

Download the code for section07.