MasterPageFile 何时位于 ASP.NET MVC WebForms ViewEngine 中

发布于 2024-08-12 16:10:55 字数 1119 浏览 8 评论 0原文

何时检查视图/页面的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

同展鸳鸯锦 2024-08-19 16:10:55

解决问题的最佳选择可能是创建一个自定义 VirtualPathProvider

Your best bet to solve the problem is probably to create a custom VirtualPathProvider

你的往事 2024-08-19 16:10:55

如果您想更改母版页的查找方式,您可以实现自己的视图引擎:

public CustomViewEngine()
{
    MasterLocationFormats = new string[] {
        "~/RealPlace/Site.Master""
    };
}

If you want to change how a masterpage is found you can implement your own viewengine:

public CustomViewEngine()
{
    MasterLocationFormats = new string[] {
        "~/RealPlace/Site.Master""
    };
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文