在通过程序集引用的内容页上引发 NullReferenceException 异常

本文关键字:异常 NullReferenceException 过程 程序集 引用 | 更新日期: 2023-09-27 18:30:47

尝试使用引用程序集中的 ASPX 页时遇到此问题。此页由内容页及其母版页组成。仅当从另一个 Web 项目访问任何内容页 Web 控件时,才会引发异常,但当从其所属的同一项目访问该页面时,不会发生这种情况。

起初,这些页面应该是常规的ASPX页面,然后它们工作得很好(即这个异常没有发生),但是我们的上级决定将它们包装到MasterPages中,出于一些可重用性或其他原因(这有点奇怪,因为这个ASPX页面是自动生成的)。

所以,我们现在遇到了这个麻烦:/

编辑:我正在添加一些代码来帮助您帮助我:)

母版页:

<%@ Master Language="C#" AutoEventWireup="true" CodeBehind="MasterPage.master.cs"
Inherits="WebApplicationTemplate.MasterPage" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
    <asp:ContentPlaceHolder ID="headPlaceHolder" runat="server">
    </asp:ContentPlaceHolder>
</head>
<body>
    <form id="form" runat="server">
        <asp:ContentPlaceHolder ID="formPlaceHolder" runat="server">
        </asp:ContentPlaceHolder>
    </form>
</body>
</html>

内容页面:

<%@ MasterType VirtualPath="~/MasterPage.Master" %>
<%@ Page Title="" Language="C#" MasterPageFile="~/MasterPage.Master" AutoEventWireup="true"
    CodeBehind="TestPage.aspx.cs" Inherits="WebApplicationTemplate.TestPage" %>
<asp:Content ID="headContent" ContentPlaceHolderID="headPlaceHolder" runat="server">
</asp:Content>
<asp:Content ID="formContent" ContentPlaceHolderID="formPlaceHolder" runat="server">
    <asp:TextBox ID="id1" runat="server" Text="Text" MaxLength="40" Style="top: 100;
        left: 100; width: 100; height: 100; position: absolute;" />
</asp:Content>

在内容页代码隐藏中引发异常的函数:

public void Foo()
{
id1.Text = "something"; //Object reference not set to an instance of an object.
}

正如我之前所说,只有当我通过引用的程序集从另一个项目访问此页面时,我才遇到此问题。我不知道我是否必须在任何 web.config 中配置某些内容,无论是在母版页项目中还是在引用前一个项目的程序集的项目中。

在通过程序集引用的内容页上引发 NullReferenceException 异常

当您

缺少要访问其程序集的其他解决方案中存在的某些配置信息时,通常会发生此类错误。

您需要在当前项目中复制这些设置(例如.app.config 或 web.config),从中访问其他程序集。

希望这有帮助。

问题解决了。

内容页面上的第一行搞砸了,所以这就是它现在的样子:

    <%@ Page Title="" Language="C#" MasterPageFile="~/MasterPage.Master" AutoEventWireup="true"
    CodeBehind="TestPage.aspx.cs" Inherits="WebApplicationTemplate.TestPage" %>
<asp:Content ID="headContent" ContentPlaceHolderID="headPlaceHolder" runat="server">
</asp:Content>
<asp:Content ID="formContent" ContentPlaceHolderID="formPlaceHolder" runat="server">
    <asp:TextBox ID="id1" runat="server" Text="Text" MaxLength="40" Style="top: 100;
        left: 100; width: 100; height: 100; position: absolute;" />
</asp:Content>

因此,在删除<%@ MasterType VirtualPath="~/MasterPage.Master" %>行后,问题就解决了,尽管现在如果想要从 ContentPage 访问其字段、属性、控件等,我们必须强制转换 Page.Master 属性,因为通过删除上述行,我们不再知道我们指的是哪个 MasterPage 类。

像这样:

    MasterPage MP;
    protected override void OnInit(EventArgs e)
    {
        base.OnInit(e);
        //Setting the master page
        MP = ((WebApplicationTemplate.MasterPage)Master);
    }
    protected void Foo()
    {
        //Accessing a MasterPage control
        MP.Bar.Visible = false;
    }

嗯,就是这样。无法猜测这如何解决我的问题,但它确实如此。如果有人能对此有所了解并满足我的好奇心,我会很高兴。这不太可能,但是,如果有人碰巧遇到这个问题,希望有人发现这很有用。