Mar*_*nox 5 asp.net-mvc viewengine razor
我正在玩剃刀视图引擎,而且还有一些我不太了解的东西.
_ViewStart文件指定具有完整文件路径的布局,如下所示:
@{
Layout = "~/Views/Shared/_MasterLayout.cshtml";
}
Run Code Online (Sandbox Code Playgroud)
据我了解,必须包括完整的路径和扩展.你不能这样做:
@{
Layout = "_MasterLayout";
}
Run Code Online (Sandbox Code Playgroud)
但是,视图引擎指定搜索主视图的位置:
MasterLocationFormats = new string[] {
"~/Views/{1}/{0}.cshtml",
"~/Views/Shared/{0}.cshtml"
};
Run Code Online (Sandbox Code Playgroud)
为什么_ViewStart文件中需要完整的主布局文件路径?
如果指定了完整路径,那么指定可能位置的重点是MasterLocationFormats[]什么?
更新
好吧,我还没有找到一个满意的答案.
从实验中可以看出,在viewstart文件中指定Layout时,MasterLocationFormats要么被插入要么被覆盖.
我可以从MasterLocationFormats中完全删除MasterLayout.cshtml位置,它对网页的显示没有任何影响.
我的个人问题是由于使用了MvcMailer软件包,它允许您指定剃刀视图以用作发送HTML电子邮件的模板.这个DOES使用MasterLocationFormats.
所以我还是有点困惑,但希望这将是一些使用到任何人来到这里.此外,这篇文章也可能有所帮助.
在 RazorViewEngine 的 CreateView 实现中,创建了一个新的 RazorView。
当 RazorView 重写 BuildManagerCompiledView 的 RenderView 方法时,它会实际调用 IView 的 Render 方法。
在此实现结束时调用该行。
webViewPage.ExecutePageHierarchy(new WebPageContext(context: viewContext.HttpContext, page: null, model: null), writer, startPage);
Run Code Online (Sandbox Code Playgroud)
这将我们引向 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;
}
Run Code Online (Sandbox Code Playgroud)
正如您在上面看到的,布局路径被覆盖。
有关更多信息,您可以查看 RazorView 和 WebViewPage 类。
| 归档时间: |
|
| 查看次数: |
1093 次 |
| 最近记录: |