主属性不是在 aspx 设计器中自动生成导致运行时错误

Posted

技术标签:

【中文标题】主属性不是在 aspx 设计器中自动生成导致运行时错误【英文标题】:Master property is not auto generated in aspx designer causing runtime errors 【发布时间】:2021-05-01 14:20:02 【问题描述】:

我有一个使用MasterPageFile 的内容页面,在代码中我们尝试访问主属性Master.SessionId

<%@ Page Title="" Language="C#" MasterPageFile="~/Admin/AdminFrontend.Master" AutoEventWireup="true"
    CodeBehind="Reasons.aspx.cs" Inherits="Admin.Other.Reasons" %>

<asp:Content ID="Content2" ContentPlaceHolderID="cphMainBody" runat="server">
    <reason session-id="<%=Master.SessionId%>">
    </reason>
</asp:Content>

Master.SessionId 无法识别,Master 没有引用正确的主文件。类似的代码适用于项目中的另一个文件。我们发现的唯一显着区别是,正常工作的页面在 aspx.designer.cs 文件中有以下自动生成的代码。

public partial class MyChart 
        
        /// <summary>
        /// Master property.
        /// </summary>
        /// <remarks>
        /// Auto-generated property.
        /// </remarks>
        public new Admin.AdminFrontend Master 
            get 
                return ((Admin.AdminFrontend)(base.Master));
            
        
    

这是我的 Reasons.aspx 文件的设计器。

//------------------------------------------------------------------------------
// <auto-generated>
//     This code was generated by a tool.
//
//     Changes to this file may cause incorrect behavior and will be lost if
//     the code is regenerated. 
// </auto-generated>
//------------------------------------------------------------------------------

namespace Admin.Other



    public partial class Reasons
    
    


I am not sure what is the problem, why in one file that property is auto-generated and not in the other. I thought Visual Studio is doing something crazy. I restarted VS2019 and also tried restarting my machine. Both didn't solve the 

问题。

【问题讨论】:

【参考方案1】:

“Reasons.aspx.cs”甚至可能没有“设计器”页面。请检查以确保它存在于 Reasons.aspx 页面(打开解决方案资源管理器并查看关联到 Reason.aspx 文件)。如果 Designer 文件不存在,请在解决方案资源管理器中选择/突出显示 Reason.aspx 文件,然后使用上方菜单栏(在 Visual Studio 中),然后选择 Project > Convert to Web Application强> 选择。如果 Projects 下拉菜单中缺少该选项,这通常表示所有 Designer 文件都已连接到您的 CodeBehind 文件。

如果您有 Reasons.aspx.designer.cs,请在此处提供代码,以便我们将其与您的 MyChart 示例中的代码进行比较。

【讨论】:

我有 Reasons.aspx.designer.cs 文件,里面没有太多内容。我已将其添加到原始问题中。【参考方案2】:

在您的 Reasons.aspx.cs 文件中, 您可以添加公共属性并返回 Master.sessionID。

例如;

public partial class MyChart
 
     public string MySession
      
       get
       
        return Master.SessionID;
       
     

在您的 aspx 文件中,使用

 <reason session-id="<%=MySession%>"></reason>

【讨论】:

以上是关于主属性不是在 aspx 设计器中自动生成导致运行时错误的主要内容,如果未能解决你的问题,请参考以下文章