Parser Error: '_Default' is not allowed here because it does not extend class 'System.Web.UI.Page' & MasterType declaration

Amanda Kitson picture Amanda Kitson · Jan 13, 2010 · Viewed 99.2k times · Source

I recently converted a website project to a web application project in Visual Studio 2008. I finally got it to compile, and the first page (the login screen) displayed as normal, but then when it redirected to the Default.aspx page, I received an error:

Parser Error Message: 'SOME.NAMESPACE.MyApplicationName.WebApplication._Default' is not allowed here because it does not extend class 'System.Web.UI.Page'.

All of my pages inherit from a class called "BasePage" which extends System.Web.UI.Page. Obviously the problem isn't with that class because the login.aspx page displays without error, and it also inherits from that basepage.

All the pages on the site, including the login page, are children of a masterpage.

After some testing, I have determined what it is that causes the error (although I don't know WHY it does it).

On all the pages where I have the following tag, the error does NOT occur.

<%@ MasterType VirtualPath="~/MasterPages/MainMaster.master" %>

On all the pages that do not contain that line, the error DOES occur. This is throughout the entire application. I have the tag only on pages where there has been a need to reference controls on the MasterPage.

So, I thought I would just add that line to all my pages and be done with it. But when I add that line, I get a compile error: 'object' does not contain a definition for 'Master'

This error is coming from the designer.cs file associated with the ASPX page that I have added the "MasterType" declaration to.

I've forced a rebuild of the designer file, but that doesn't change anything. I compared the content of the Master reference in the designer files between the login.aspx (working) and the default.aspx (not working) but they are exactly the same.

Since I'd really like to get it to work without having to add the "MasterType" declaration to everypage, and since that "fix" isn't working anyway, does anyone know why not having the "MasterType" declaration on an aspx file causes the parser error? Is there a fix for this?

Example Code:

Here is the code for login.aspx and login.aspx.cs which is working without error:

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>

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");
        }
    } 
}

Here is the code for default.aspx and default.aspx.cs which is throwing the parser error when viewed in a web browser:

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>

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)
    {
    }
}

Thanks!

Answer

Amanda Kitson picture Amanda Kitson · Jan 13, 2010

I figured it out. The problem was that there were still some pages in the project that hadn't been converted to use "namespaces" as needed in a web application project. I guess I thought that it wouldn't compile if there were still any of those pages around, but if the page didn't reference anything from outside itself it didn't appear to squawk. So when it was saying that it didn't inherit from "System.Web.UI.Page" that was because it couldn't actually find the class "BasePage" at run time because the page itself was not in the WebApplication namespace. I went through all my pages one by one and made sure that they were properly added to the WebApplication namespace and now it not only compiles without issue, it also displays normally. yay!

what a trial converting from website to web application project can be!