Está en la página 1de 3

2/7/2020 ASP.

NET Core - Razor View Start - Tutorialspoint

ASP.NET Core - Inicio de Razor View

En este capítulo, discutiremos el Inicio de la Vista Razor. El motor de vista Razor en MVC tiene
una convención donde buscará cualquier archivo con el nombre _ViewStart.cshtml y ejecutará
el código dentro de este archivo. antes de ejecutar el código dentro de una vista individual.

El código dentro del archivo ViewStart no se puede procesar en la salida HTML de una
página, pero se puede usar para eliminar el código duplicado de los bloques de código
dentro de las vistas individuales.
En nuestro ejemplo, si queremos que cada vista use la vista Diseño que hemos creado
en el último capítulo, podríamos poner el código para establecer la vista Diseño dentro
de un ViewStart en lugar de tener el código dentro de cada vista.

Ejemplo
Tomemos un ejemplo simple para ver cómo funciona esto. En nuestra aplicación, no queremos
que cada vista especifique que su vista de Diseño es _Layout.cshtml . Así que haga clic
derecho en la carpeta Vistas y seleccione Agregar → Nuevo elemento .

https://www.tutorialspoint.com/asp.net_core/asp.net_core_razor_view_start.htm 1/3
2/7/2020 ASP.NET Core - Razor View Start - Tutorialspoint

There is a specific template in ASP.NET MVC for a ViewStart page, so select MVC View Start
Page in the middle pane. The most important part here is that this file is named
_ViewStart.cshtml. Now click on the Add button.

The primary use of a ViewStart file is to set the Layout view.


Let us now go to the Index.cshtml file and cut the Layout line and then add it to the ViewStart
file as shown in the following program.

https://www.tutorialspoint.com/asp.net_core/asp.net_core_razor_view_start.htm 2/3
2/7/2020 ASP.NET Core - Razor View Start - Tutorialspoint

@{
Layout = "~/Views/Shared/_Layout.cshtml";
}

When the MVC framework goes to render a view, it will see if there Is a ViewStart file
somewhere in the folder hierarchy.
We have placed _ViewStart directly into our Views folder. This is going to impact all the
views in all the folders that are inside the Views folder, and both the views inside the
Home folder, as well as the Shared folder, as well as any other controller folders that we
might add in the future.
If we take ViewStart and place it only in the Home folder, then this little bit of code
would only execute when we are rendering one of those views in the Home folder.
We can even have multiple ViewStart files, so we could have a ViewStart.cshtml here in
the Views folder that sets the Layout view for all views.
But if we wanted to change that default for all of the views just in the Home folder, we
could have another ViewStart in the Home folder that sets the layout to something else.
Let us save all the files and run the application.

Verá que su página de inicio todavía se presenta de la misma manera que antes, y todavía
tenemos vigente la vista Diseño.

https://www.tutorialspoint.com/asp.net_core/asp.net_core_razor_view_start.htm 3/3

También podría gustarte