使用_viewstart.cshtml和部分刀片观点的正确方法? [英] Correct way to use _viewstart.cshtml and partial Razor views?
问题描述
我用<一个href=\"http://weblogs.asp.net/scottgu/archive/2010/10/22/asp-net-mvc-3-layouts.aspx\">_viewstart.cshtml相同的剃刀布局自动地分配给我的意见。
I'm using _viewstart.cshtml to automagically assign the same Razor Layout to my views.
这是在我的浏览文件夹的根死简单的文件看起来像这样:
It's a dead simple file in the root of my Views folder that looks like this:
@{
Layout = "~/Views/Shared/_Layout.cshtml";
}
这是不是添加@Layout指令,每一个观点更加干燥。
This is more DRY than adding the @Layout directive to every single view.
然而,这造成了剃刀的部分意见的问题,因为它们运行_viewstart.cshtml的内容,因此错误地分配自己的布局,这使得他们,嗯,不再偏。
However, this poses a problem for Razor partial views, because they run the contents of _viewstart.cshtml and therefore incorrectly assign themselves a layout, which makes them, um, no longer partial.
下面是一个假设的项目,表示_viewstart.cshtml文件,共享_layout.shtml文件,和一个局部视图(AnonBar.cshtml)。
Here's a hypothetical project, showing the _viewstart.cshtml file, the shared _layout.shtml file, and a partial view ("AnonBar.cshtml").
目前,我正在得到解决这个问题的方法是添加以下行的每一个局部视图:
Currently, the way that I'm getting around this is by adding the following line to every partial view:
@{
Layout = "";
}
这似乎是一个错误的方式来表示一个视图中的剃刀部分。 (请注意,与Web窗体视图引擎,文件扩展名是局部的观点是一样的。)
This seems like the wrong way to denote a view as a partial in Razor. (Note that unlike the web forms view engine, the file extension is the same for partial views.)
我考虑其他选项,但那些更糟糕的:
Other options I considered but that are even worse:
- 把所有的局部视图到公共文件夹中,这样他们就可以共享一个共同的_viewstart.cshtml。这打破了意见在同一文件夹作为他们的控制器是该公约。
- 不使用局部视图。
这是不是一个正在用的Razor视图引擎团队充实,还是我失去了一个基本的概念?
Is this something that is still being fleshed out by the Razor view engine team, or am I missing a fundamental concept?
推荐答案
如果您返回PartialView()
从控制器(而不是返回查看()
),那么 _viewstart.cshtml
将不被执行。
If you return PartialView()
from your controllers (instead of return View()
), then _viewstart.cshtml
will not be executed.
这篇关于使用_viewstart.cshtml和部分刀片观点的正确方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!