Total Page Preview:   000000000166

What is Layout view in Asp.Net Core MVC

 Layout View n Asp.Net core MVC 

In ASP.NET Core MVC, a layout view is a shared Razor view that defines the common structure and layout of your web application. It typically contains the HTML structure, header, footer, and any other common elements that are shared across multiple pages of your website. Using a layout view allows you to maintain a consistent look and feel throughout your application.

Here's an example of how to create and use a layout view in ASP.NET Core MVC:
1-Create a new ASP.NET Core MVC project or open an existing one.
2-In the "Views" folder, create a new folder (if it doesn't already exist) called "Shared."
3-Inside the "Shared" folder, create a new Razor view called "_Layout.cshtml." The underscore at the beginning of the filename indicates that it's a partial view, and it won't be directly accessible as a standalone page.
4-Edit the "_Layout.cshtml" file to define the common layout structure. Here's a simple example:
In this example, we have a common header, navigation menu, and footer. The @RenderBody() method is used to render the content of individual views within the layout.
1-Now, you can create individual views that use this layout. For example, create a "Home" view
  • In the "Views" folder, create a new folder called "Home."
  • Inside the "Home" folder, create a Razor view called "Index.cshtml."
  • Edit the "Index.cshtml" file to define the content for the home page:
2-Similarly, create other views like "About" and "Contact" following the same steps as above.
3-In your controller actions, specify the layout to be used by setting the Layout property of the ViewResult:
That's it! Now, when you navigate to these views (e.g., /Home/Index, /Home/About, /Home/Contact), they will use the common layout defined in "_Layout.cshtml" to wrap their content, providing a consistent look and feel across your application.

 

 

 

Thank You

About Author

Brijesh Kumar

Database Developer

I have more then 6 years Experience in Microsoft Technologies - SQL Server Database, ETL Azure Cloud - Azure SQL Database, CosmosDB, Azure Data Factory, PowerBI, Web Job, Azure Function, Azure Storage, Web Apps, Powershall and Database Migration On-Premise to Azure Cloud.
LinkedIn : https://www.linkedin.com



Comments


                           
                           

                           

                           

Facebook User: