当在视图引擎中指定位置时,为什么在viewstart文件中需要布局的完整路径? [英] Why is a full path to layout required in viewstart file when locations are specified in the view engine?

查看:70
本文介绍了当在视图引擎中指定位置时,为什么在viewstart文件中需要布局的完整路径?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用剃刀视图引擎,但有一些我不太了解的东西.

I am playing with the razor view engine and there's something I don't quite get.

_ViewStart文件指定具有完整文件路径的布局,如下所示:

The _ViewStart file specifies a layout with the full file path like this:

@{
    Layout = "~/Views/Shared/_MasterLayout.cshtml";
}

据我了解,必须包括完整路径和扩展名.您 不能 这样做:

As I understand it, the full path and extension must be included. You can't just do this:

@{
    Layout = "_MasterLayout";
}

但是,视图引擎会指定搜索主视图的位置:

However the view engine specifies locations to search for the master views:

        MasterLocationFormats = new string[] { 
            "~/Views/{1}/{0}.cshtml",
            "~/Views/Shared/{0}.cshtml"
        };

为什么在_ViewStart文件中需要主布局文件的完整路径?

How come the full path to the master layout file is required in the _ViewStart file?

如果指定了完整路径,那么在MasterLocationFormats[]中指定可能位置的意义何在?

And if the full path is specified, what is the point of then specifying possible locations in MasterLocationFormats[]?

更新

我还没有找到令人满意的答案.

Well I still haven't found a satisfactory answer to this.

根据实验,当在viewstart文件中指定布局时,似乎MasterLocationFormats是已插入已覆盖.

From experimenting it would appear that the MasterLocationFormats are either ingored or overridden when specifying a Layout in the viewstart file.

我可以从MasterLocationFormats中完全删除MasterLayout.cshtml位置,它对网页的显示没有任何影响.

I could completely remove the MasterLayout.cshtml location from the MasterLocationFormats and it didn't make any difference to the display of web pages.

我的个人问题是由于使用了 MvcMailer软件包,它允许您指定一个剃刀视图,以用作发送html电子邮件的模板.这确实使用MasterLocationFormats.

My personal question was due to using the MvcMailer package, which allows you to specify a razor view to use as a template for sending html email. This DOES use the MasterLocationFormats.

所以我仍然有些困惑,但是希望这对来这里的人有帮助.另外,这篇文章也可能会有所帮助.

So I'm still a bit perplexed, but hope this will be of some use to anybody coming here. Also , this post may also be of help.

推荐答案

在RazorViewEngine的CreateView实现中,将创建一个新的RazorView.

In CreateView implementation of RazorViewEngine a new RazorView is created.

并且当RazorView覆盖BuildManagerCompiledView的RenderView方法时,它将实际调用IView的Render方法.

And when RazorView overrides RenderView method of BuildManagerCompiledView which makes the actual call to Render method of IView.

在此实现的最后,将调用该行.

And at the end of this implementation that line is called.

webViewPage.ExecutePageHierarchy(new WebPageContext(context: viewContext.HttpContext, page: null, model: null), writer, startPage);

这导致我们转到System.Web.Mvc.dll中的WebViewPage的ExecutePageHierarchy方法.

And that leads us to ExecutePageHierarchy method of WebViewPage which is in System.Web.Mvc.dll.

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;
}

如您所见,上面的布局路径已被覆盖.

As you can see above Layout path is overriden.

有关更多信息,您可以检查RazorView和WebViewPage类.

For more information, you can check RazorView and WebViewPage classes.

这篇关于当在视图引擎中指定位置时,为什么在viewstart文件中需要布局的完整路径?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆