Tuesday, 22 March 2022

ViewStart.cshtml in ASP.NET Core MVC

 # _ViewStart.cshtml in ASP.NET Core MVC

1. We use the Layout property to associate a view with a layout view. Without the _ViewStart.cshtml file, we set the Layout property in each and every view. This violates DRY (Don't Repeat Yourself) principle, and has the following disadvantages
    Redundant code.
    Maintenance overhead.
2. It's a special file in ASP.NET Core MVC. The code in this file is executed before the code in an individual view is executed. So this means, instead of setting the Layout property in each individual view, we can move that code into the _ViewStart.cshtml file.
@{
    Layout = "_Layout";
}
3. By setting the Layout property in _ViewStart.cshtml file maintaining our application becomes much easier. In the future, if we want to use a different layout file we just need to change the code in one place in _ViewStart.cshtml.
4. If you want to use a layout file that is different from what is specified in _ViewStart.cshtml, you can do so by setting the Layout property in an individual view. You can also set the Layout property to null, if you want your view to be rendered without a layout view.
5. We can also have different ViewStart file. Store it in different views folder. Only views present in that folder will get affected due to this.

No comments:

Post a Comment

Search This Blog

Creating your first "Alexa" Skill

Index What is Alexa What is Alexa Skill? Why is it required when Alexa already equipped with voice assistant? Dev...