我正在玩剃刀视图引擎,而且还有一些我不太了解的东西.
_ViewStart文件指定具有完整文件路径的布局,如下所示:
@{ Layout = "~/Views/Shared/_MasterLayout.cshtml"; }
据我了解,必须包括完整的路径和扩展.你不能这样做:
@{ Layout = "_MasterLayout"; }
但是,视图引擎指定搜索主视图的位置:
MasterLocationFormats = new string[] { "~/Views/{1}/{0}.cshtml","~/Views/Shared/{0}.cshtml" };
为什么_ViewStart文件中需要完整的主布局文件路径?
如果指定了完整路径,那么在MasterLocationFormats []中指定可能的位置有什么意义呢?
更新
好吧,我还没有找到一个满意的答案.
从实验中可以看出,在viewstart文件中指定Layout时,MasterLocationFormats要么被插入要么被覆盖.
我可以从MasterLocationFormats中完全删除MasterLayout.cshtml位置,它对网页的显示没有任何影响.
我的个人问题是由于使用了MvcMailer package,它允许您指定剃刀视图以用作发送HTML电子邮件的模板.这个DOES使用MasterLocationFormats.
所以我仍然有点困惑,但希望这对任何来这里的人都有用.另外,this post may also be of help.
解决方法
在RazorViewEngine的CreateView实现中,创建了一个新的RazorView.
当RazorView覆盖BuildManagerCompiledView的RenderView方法时,它实际调用了IView的Render方法.
在此实现结束时,该行被调用.
webViewPage.ExecutePageHierarchy(new WebPageContext(context: viewContext.HttpContext,page: null,model: null),writer,startPage);
这导致我们在System.Web.Mvc.dll中的WebViewPage的ExecutePageHierarchy方法.
public override void ExecutePageHierarchy() { TextWriter writer = this.ViewContext.Writer; this.ViewContext.Writer = this.Output; base.ExecutePageHierarchy(); if (!string.IsNullOrEmpty(this.OverridenLayoutPath)) this.Layout = this.OverridenLayoutPath; this.ViewContext.Writer = writer; }
如您所见,上面的布局路径被覆盖.
有关更多信息,您可以检查RazorView和WebViewPage类.