Question:
I'm using Microsoft Visual Studio 2013 and created a C# ASP.NET MVC 4 project.
I created a Master Page in the Views directory, called Modelo.master
. Then I created a Controller in the Controllers
directory, and again a View that would display the Controller output.
However, on the Views creation screen where the program asks me to select a Master Page or layout to use as the base of the page, I can't see the Master Page I created. The file Modelo.master
n't even appear in the list.
What could it be?
Answer:
On Razor, the Master Page
template is not used. What exists are Layout files, used by the View
to build itself.
Unlike Master Pages , where content sections were specified using its own set of tags, Razor uses pure HTML, processing notations and returning more HTML. The approach is simpler and less error-prone on the part of the server.
Normally the main view Layout file (that is, the one that opens in a computer browser) is inside Views\Shared
with the name of _Layout.cshtml
, but nothing prevents you from modifying that name. In this directory, other Layout files common to the system must be placed. Layouts that belong to certain Views belonging to a Controller can be located in the specific directory of those Views .
The Wizard you ran just generates a View with the following statement, which is completely optional:
@{
Layout = "~/Views/Caminho/Da/Minha/View/_Layout.cshtml";
}
This declaration is also found in the special View _ViewStart.cshtml
, which starts a default Layout if no Layout is specified:
@{
Layout = "~/Views/Shared/_Layout.cshtml";
}