Getting Started with Asp.Net Core (V) - Layout View _Layout.cshtml

  Layout view and we Asp.Net MVC same view _Layout.cshtml layout makes it easier to view all consistent look, because we only want to modify a layout view file, change will be reflected immediately in the entire application all views.

  In ASP.NET Core MVC, some view files, such as file name other .cshtml document view layout, _ViewStart.cshtml and _ViewImports.cshtml begin with an underscore, etc., before these underscore in the file name indicates that these files are not directly for the browser.

  We can contain multiple layout view files in a single application. For example, a layout view file services for the administrator user, the other a different layout view file services to ordinary users.

  We will generally be built in the Layout view Views / Shared folder to _Layout.cshtml name.

<! DOCTYPE HTML> 

<HTML> 
<head> 
    <Meta name = " the viewport " Content = " width = width-Device " /> 
    <title> ViewBag.Title @ </ title> 
</ head> 
<body> 
    <div> 
        <-! @ RenderBody () is injected into a specific location of the content view. For example, if you use this layout view showing index.chtml view, it will inject index.cshtml view the contents of the position we call @RenderBody () method. -> 
        @RenderBody ()
     </ div> 

    @ * @ IF (IsSectionDefined ( " the Scripts " )) 
    { 
        @RenderSection ( " the Scripts " );
"Scripts", false);

</body>
</html>

  We can specify which page layout enabled in Views / _ViewStart.cshtml, because when the request will first find _ViewStart.cshtml.

@{
    Layout = "_Layout";
}

@if (User.IsInRole("Admin"))
{
    Layout = "_AdminLayout";
}
else
{
    Layout = "_NoAdminLayout";
}

  At the same time, if we use the same namespace in a lot of pages, we can add a shared namespace Views / _ViewImports.cshtml file in the same model, model.

StudentManagement.Models @using; 
@using StudentManagement.ViewModels; 

@ * also supports the following command * @ 
@ * 
    @addTagHelper 
    @removeTagHelper 
    @tagHelperPrefix 
    @model 
    @inherits 
    @Inject
 * @

  Note that, _ViewStart and _ViewImports support hierarchical, except put it in a folder other than the folder Views, we can also "Home" subfolders Views folder to place another _ViewImports folder in the file Home folder of \_ViewImportsthe cover in the Shared folder \_ViewImportsfile specified settings.

Guess you like

Origin www.cnblogs.com/jesen1315/p/11041967.html