MasterPageFile 何时位于 ASP.NET MVC WebForms ViewEngine 中
何时检查视图/页面的 MasterPageFile 属性是否存在于 ASP.NET MVC WebForms 视图引擎中?
我想要做的是让以下代码不输出错误:
解析器错误消息:文件“/SomePlaceThatDosentExist/Site.Master”不存在。
在我的视图的 .aspx 文件中如此定义:
<%@ Page Language="C#" MasterPageFile="~/SomePlaceThatDosentExist/Site.Master" Inherits="System.Web.Mvc.ViewPage" >
我需要在哪里编写一些代码才能进入并定义有效的 MasterPageFile 属性?
我在我的视图继承的自定义 ViewPage 类中尝试了以下操作
public override string MasterPageFile
{
get
{
return base.MasterPageFile;
}
set
{
base.MasterPageFile = "~/RealPlace/Site.Master";
}
}
,并尝试了以下操作(在我的视图继承的自定义视图页面类中)
protected override void OnPreInit(EventArgs e)
{
base.MasterPageFile = "~/RealPlace/Site.Master";
base.OnPreInit(e);
}
在这两种情况下,都会显示我上面所述的错误。
据我所知,OnPreInit 是 ViewPage 生命周期中最早的点,那么是否有可能在生命周期中走得更早?
在写和回答之前请注意:
- 我知道 return View("ViewName", "MasterPageName");
- 我了解动态母版页,但我想完成这个特定任务
When is the MasterPageFile property of a view/page checked that it exists in ASP.NET MVC WebForms view engine?
What I want to do is have the following code not output the error:
Parser Error Message: The file '/SomePlaceThatDosentExist/Site.Master' does not exist.
Defined as such in my view's .aspx file:
<%@ Page Language="C#" MasterPageFile="~/SomePlaceThatDosentExist/Site.Master" Inherits="System.Web.Mvc.ViewPage" >
Where would I need to write some code to go in and define a valid MasterPageFile property?
I have tried the following in my custom ViewPage class that my views inherit
public override string MasterPageFile
{
get
{
return base.MasterPageFile;
}
set
{
base.MasterPageFile = "~/RealPlace/Site.Master";
}
}
and tried the following also (in a custom view page class that my views inherit)
protected override void OnPreInit(EventArgs e)
{
base.MasterPageFile = "~/RealPlace/Site.Master";
base.OnPreInit(e);
}
In both cases, the error I stated above is displayed.
From what I know, OnPreInit is the earliest point in a ViewPage's lifecycle, so is it possible to go even earlier in the lifecycle?
Note before you write and answer:
- I know about return View("ViewName", "MasterPageName");
- I know about dynamic master pages, but I want to accomplish this specific task
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
解决问题的最佳选择可能是创建一个自定义 VirtualPathProvider
Your best bet to solve the problem is probably to create a custom VirtualPathProvider
如果您想更改母版页的查找方式,您可以实现自己的视图引擎:
If you want to change how a masterpage is found you can implement your own viewengine: