ViewStart和Layout-有什么区别? [英] ViewStart and Layout - what's the difference?
问题描述
我刚刚开始阅读ASP.NET MVC 4书籍,但遇到了一个问题.在Razor部分中,作者说,如果我未在View.cshtml文件中设置Layout变量,它将默认搜索_ViewStart.cshtml,但在另一部分中,他在Views/Shared中使用_Layout.cshtml,但我没有得到它
I have just started reading ASP.NET MVC 4 book and got one question. In the Razor part author said that if i dont set the Layout variable in the View.cshtml file it will as default search for a _ViewStart.cshtml, but in another part he is using _Layout.cshtml in Views/Shared, I dont get it.
如果我在View.cshtml中有这样的东西:
If i got smth like this in View.cshtml :
@{
Layout = null;
}
它告诉我们该视图没有布局,但是如果我使它像这样:
It tells that that this View has no layout, but if i make it like :
@{
}
因此,这将使View搜索_ViewStart.cshtml文件或_Layout.cshtml吗?
So this will make that the View will search for a _ViewStart.cshtml file or _Layout.cshtml?
推荐答案
基本上,默认情况下,我们在视图>>共享>> _Layout.cshtml 中具有主布局,并且在 _ViewStart.cshtml ,这是我们的默认主版面.
Basically by default we have master layout in Views>> Shared >> _Layout.cshtml and this thing is defined in _ViewStart.cshtml that which one is our default master layout.
默认情况下,当我们使用主布局创建视图时,其主布局为 _Layout.cshtml ,但是我们可以从 _ViewStart.cshtml
When we create a view with master layout by default its master layout is _Layout.cshtml, but we can change it from _ViewStart.cshtml
我们写的时候:
@{
Layout = null;
}
在我们的视图中,我们说该视图没有任何主布局,这在我们主要创建部分视图或没有主布局的独立视图时使用.
in our view we say that this view does not have any master layout, this is used when we create partial view mostly or a standalone view without master layout.
如果您默认打开 _ViewStart.cshtml ,则其中会写有以下内容:
If you open _ViewStart.cshtml by default it has this written in it:
@{
Layout = "~/Views/Shared/_Layout.cshtml";
}
我们可以根据需要进行更改.
and we can change it if we want to.
您还可以通过在主布局视图上编写URL来更改某些特定视图的主布局:
You can also change of some specific view Master Layout by writing on top of it the url of master layout view:
@{
Layout = "~/Views/Shared/_CustomMasterLayout.cshtml";
}
这篇关于ViewStart和Layout-有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!