Introduction to layout pages of ASP. NET Core 2.0 and asp. netcore
This article introduces the layout page of ASP. NET Core 2.0, which is as follows:
Problem
In ASP. NET Core 2.0, how does one share visible elements, code blocks, and commands?
Answer
To create an empty project, first add the GreetingService and UserViewModel:
public interface IGreetingService{ string Greet(string firstname, string surname);} public class GreetingService : IGreetingService{ public string Greet(string firstname, string surname) { return $"Hello {firstname} {surname}"; }}
Add the MVC service and GreetingService service to Startup and configure the MVC middleware:
public void ConfigureServices(IServiceCollection services){ services.AddScoped<IGreetingService, GreetingService>(); services.AddMvc();} public void Configure(IApplicationBuilder app, IHostingEnvironment env){ if (env.IsDevelopment()) { app.UseDeveloperExceptionPage(); } app.UseMvc(routes => { routes.MapRoute( name: "default", template: "{controller=Home}/{action=Index}/{id?}"); });}
Add the Controller HomeController, modify the Index method, and return the ViewResult:
public class HomeController : Controller{ public IActionResult Index() { var model = new UserViewModel { Firstname = "Tahir", Surname = "Naushad" }; return View(model); }}
Add Layout page (_ Layout. cshtml ):
<!DOCTYPE html>
To add a view, follow the naming conventions (Views/Home/Index. cshtml ):
@model UserViewModel@{ ViewBag.Title = "ASP.NET Core";}
Add the import page (_ ViewImports. cshtml ):
@using LayoutPage.Models@inject IGreetingService Greeter
Add the start page (_ ViewStart. cshtml ):
@{ Layout = "_Layout";}
The directory structure is as follows:
Run:
Discussion
ASP. NET Core provides a way to reuse visible elements and public code between different views:
1. layout page
2. Start page
3. Import page
Layout page (_ Layout. cshtml)
A Layout page is used to share common visible elements between different pages to provide consistent appearances and user experience for the entire application.
The Layout page is added to the Views/Shared directory and named _ Layout. cshtml (agreed rule ). Multiple layout pages can be placed in one application.
A view has a Layout attribute to set the Layout to use. ASP. NET Core first searches for the layout of the view-related folders. If not, it searches for the layout in the Shared directory. The layout page calls the @ RenderBody method to render the View content.
If _ Layout. cshtml is deleted, we can see the order of the search path from the exception information:
You can also use @ RenderSection to determine which section in the view to replace. These paragraphs can be mandatory or optional. The view uses @ section to define the content of these paragraphs. On the layout page, you can use IsSectionDefined to determine whether a section is defined in the view and process the section based on the judgment result:
@if (IsSectionDefined("links")){ @RenderSection("links", required: false)}else{ <em>No social media links supplied</em>}
Import page (_ ViewImports. cshtml)
We have discussed in the previous article that views can use commands to do many things, such as importing namespaces (@ using) and injecting dependencies (@ inject) and declare the model type (@ model ). MVC also provides an import page to Declare Public commands for one or more views.
The import page is generally added to the Views directory and named _ ViewImports. cshtml. It can also be added to other directories (such as view directories). In this case, it will be applied to the Views (including subdirectories) under this directory ).
If there are multiple import pages, use the commands closest to the view (for example, @ model, @ inject). The other case is that all commands are merged together (for example, @ using, @ addTagHelper ).
Start page (_ ViewStart. cshtml)
MVC provides a mechanism to run code before all views, which is the starting page. The start page runs before each view, except for layout pages and some views.
The start page is generally added to the Views directory and named as _ ViewStart. cshtml. If multiple start pages exist, they are executed in hierarchical order, from the root directory to the subdirectory.
The start page is usually used to set the layout page for all views in the directory.
Source code download
Original article: https://tahirnaushad.com/2017/08/23/asp-net-core-2-0-mvc-layout-pages/
The above is all the content of this article. I hope it will be helpful for your learning and support for helping customers.