分析器错误:此处不允许使用'_Default',因为它不会扩展类'System.Web.UI.Page'和MasterType声明

Ama*_*son 42 c# asp.net master-pages

我最近将一个网站项目转换为Visual Studio 2008中的一个Web应用程序项目.我终于将其编译,并且第一页(登录屏幕)正常显示,但是当它重定向到Default.aspx页面时,我收到了一个错误:

Parser Error Message: 'SOME.NAMESPACE.MyApplicationName.WebApplication._Default' is not allowed here because it does not extend class 'System.Web.UI.Page'.
Run Code Online (Sandbox Code Playgroud)

我的所有页面都继承自一个名为"BasePage"的类,它扩展了System.Web.UI.Page.显然问题不在于该类,因为login.aspx页面显示没有错误,并且它也从该基页继承.

网站上的所有页面(包括登录页面)都是母版页的子页面.

经过一些测试,我已经确定导致错误的是什么(虽然我不知道为什么会这样做).

在我有以下标记的所有页面上,不会发生错误.

<%@ MasterType VirtualPath="~/MasterPages/MainMaster.master" %>
Run Code Online (Sandbox Code Playgroud)

在所有不包含该行的页面上,都会出现错误.这贯穿整个应用程序.我只在需要在MasterPage上引用控件的页面上有标记.

所以,我想我会把这一行添加到我的所有页面并完成它.但是当我添加该行时,我得到一个编译错误:'object'不包含'Master'的定义

此错误来自与我添加了"MasterType"声明的ASPX页面关联的designer.cs文件.

我强制重建设计器文件,但这并没有改变任何东西.我比较了login.aspx(工作)和default.aspx(不工作)之间的设计器文件中的Master引用的内容,但它们完全相同.

因为我真的想让它工作而不必将"MasterType"声明添加到每个页面,并且由于"修复"无论如何都没有用,有没有人知道为什么不在aspx文件上使用"MasterType"声明导致解析器错误?有没有解决这个问题?

示例代码:

以下是login.aspx和login.aspx.cs的代码,它可以正常工作:

为Login.aspx

    <%@ Page Title="" Language="C#" MasterPageFile="~/MasterPages/MainMaster.master" AutoEventWireup="true" Inherits="SOME.NAMESPACE.MyApplicationName.WebApplication.Login" Codebehind="Login.aspx.cs" %>
<%@ MasterType VirtualPath="~/MasterPages/MainMaster.master" %>

<asp:Content ID="Content1" ContentPlaceHolderID="ContentPlaceHolder" Runat="Server">
    <table>
    <tr>
        <td>
            <asp:UpdatePanel ID="upLogin" runat="server">
                <ContentTemplate>
                    <asp:Panel ID="Panel1" runat="server" DefaultButton="Login1$LoginButton">
                        <asp:Login ID="Login1" runat="server" LoginButtonStyle-CssClass="button" 
                        TextBoxStyle-CssClass="textBoxRequired" 
                        TitleTextStyle-CssClass="loginTitle"  >
                        </asp:Login>
                    </asp:Panel>
                </ContentTemplate>
            </asp:UpdatePanel>
            <asp:UpdatePanel ID="upPasswordRecovery" runat="server">
                <ContentTemplate>
                <asp:PasswordRecovery ID="PasswordRecovery1" runat="server" 
                SubmitButtonStyle-CssClass="button" TitleTextStyle-CssClass="loginTitle" 
                SuccessText="Your new password has been sent to you."
                UserNameInstructionText="Enter your User name to reset your password." />
                </ContentTemplate>
            </asp:UpdatePanel>
        </td>
    </tr>
    </table>
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="SideBarPlaceHolder" Runat="Server">
    <h2>Login</h2>
    <asp:Button ID="btnCreateAccount" runat="server" Text="Create Account" OnClick="btnCreateAccount_Click" CausesValidation="false" />
</asp:Content>
Run Code Online (Sandbox Code Playgroud)

Login.aspx.cs

    using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using SOME.NAMESPACE.MyApplicationName.WebApplication;
using SOME.NAMESPACE.MyApplicationName.Bll;

namespace SOME.NAMESPACE.MyApplicationName.WebApplication
{
    public partial class Login : BasePage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            Login1.Focus();
        }
        protected void btnCreateAccount_Click(object sender, EventArgs e)
        {
            Page.Response.Redirect("~/CreateUser/default.aspx");
        }
    } 
}
Run Code Online (Sandbox Code Playgroud)

以下是default.aspx和default.aspx.cs的代码,它在Web浏览器中查看时抛出了解析器错误:

Default.aspx的

    <%@ Page Title="" Language="C#" MasterPageFile="~/MasterPages/MainMaster.master" AutoEventWireup="True" Inherits="SOME.NAMESPACE.MyApplicationName.WebApplication._Default" Codebehind="Default.aspx.cs" %>
<%@ MasterType VirtualPath="~/MasterPages/MainMaster.master" %>
<asp:Content ID="MainContent" ContentPlaceHolderID="ContentPlaceHolder" Runat="Server">
<div class="post">
    <h2 class="title">Announcements</h2>
    <p class="meta">Posted by Amanda Myer on December 15, 2009 at 10:55 AM</p>
    <div class="entry">
        <p>The MyApplicationName CMDB will be down for maintenance from 5:30 PM until 6:30 PM on Wednesday, December 15, 2009.</p>
    </div>
    <p class="meta">Posted by Amanda Myer on December 01, 2009 at 1:23 PM</p>
    <div class="entry">
        <p>The MyApplicationName CMDB is officially live and ready for use!</p>
    </div>
</div>
</asp:Content>
<asp:Content ID="SideBarContent" ContentPlaceHolderID="SideBarPlaceHolder" Runat="Server">
    <img src="images/MyApplicationName.jpg" alt="MyApplicationName Gremlin" width="250"/>
</asp:Content>
Run Code Online (Sandbox Code Playgroud)

Default.aspx.cs

    using System;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using SOME.NAMESPACE.MyApplicationName.Bll;
using SOME.NAMESPACE.MyApplicationName.WebApplication;

public partial class _Default : BasePage
{
    protected void Page_Load(object sender, EventArgs e)
    {
    }
}
Run Code Online (Sandbox Code Playgroud)

谢谢!

Ama*_*son 59

我想到了.问题是项目中仍有一些页面尚未转换为在Web应用程序项目中根据需要使用"名称空间".我想如果还有任何这些页面,我认为它不会编译,但如果页面没有从外部引用任何东西它似乎没有发出尖叫声.所以当它说它没有从"System.Web.UI.Page"继承时,因为它在运行时实际上找不到类"BasePage",因为页面本身不在WebApplication命名空间中.我逐个浏览了所有页面并确保它们已正确添加到WebApplication命名空间中,现在它不仅可以编译而没有问题,它也可以正常显示.好极了!

什么是从网站转换为Web应用程序项目的试用版!


Mik*_*ill 12

我有这个问题,因为我已经从我的一个ASP.Net应用程序复制了一个(相当通用的)网页到一个新的应用程序.

我更改了相关的命名空间命令,以反映文件的新位置......但是......我忘了更改aspx页面本身的Inherits参数.

<%@ Page MasterPageFile="" StylesheetTheme="" Language="C#"
    AutoEventWireup="true" CodeBehind="MikesReports.aspx.cs" 
    Inherits="MikesCompany.MikesProject.MikesReports" %>
Run Code Online (Sandbox Code Playgroud)

一旦我更改了Inherits参数,错误就消失了.