C# 解析器错误:此处不允许使用“_Default”,因为它不扩展类“System.Web.UI.Page”和 MasterType 声明

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2057817/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 23:07:04  来源:igfitidea点击:

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

c#asp.netmaster-pages

提问by Amanda Kitson

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:

我最近在 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'.

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.

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

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.

在不包含该行的所有页面上,都会发生错误。这贯穿整个应用程序。我只在需要引用 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.

此错误来自与我添加了“MasterType”声明的 ASPX 页面相关联的 Designer.cs 文件。

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.

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

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?

由于我真的很想让它工作而不必向每个页面添加“MasterType”声明,并且由于该“修复”无论如何都不起作用,有谁知道为什么不在 aspx 文件中添加“MasterType”声明导致解析器错误?有没有办法解决这个问题?

Example Code:

示例代码:

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

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

Login.aspx

登录.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

登录.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 和 default.aspx.cs 的代码,在 Web 浏览器中查看时会抛出解析器错误:

Default.aspx

默认.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

默认.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!

谢谢!

采纳答案by Amanda Kitson

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!

我想到了。问题是项目中仍有一些页面没有根据 Web 应用程序项目的需要转换为使用“命名空间”。我想我认为如果周围还有这些页面,它就不会编译,但是如果页面没有从外部引用任何内容,它似乎不会发出吱吱声。因此,当它说它不是从“System.Web.UI.Page”继承时,那是因为它在运行时实际上无法找到类“BasePage”,因为页面本身不在 WebApplication 命名空间中。我一一浏览了所有页面,并确保它们已正确添加到 WebApplication 命名空间,现在它不仅可以正常编译,而且还可以正常显示。好极了!

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

从网站到 Web 应用程序项目的转换是多么的尝试啊!

回答by DGK

Look for this:

寻找这个:

ANY page in your project that has a missing, or different Namespace...

项目中任何缺少或不同命名空间的页面...

If you have ANY page in your project with <NO Namespace> , OR a

如果您的项目中有任何带有<NO Namespace> 或 a 的页面

DIFFERENT Namespace than Default.aspx, you will get this

与 Default.aspx 不同的命名空间,你会得到这个

"Cannot load Default.aspx", or this: "Default.aspx does not belong here".

“无法加载 Default.aspx”,或者:“Default.aspx 不属于此处”。

ALSO: If you have a Redirect to a page in your Solution/Project and the page which is to be Redirected To has a bad namespace -- you may not get a compiler error, until you try and run. If the Redirect is removed or commented-out, the error goes away...

另外:如果您有一个重定向到您的解决方案/项目中的页面,并且要重定向到的页面有一个错误的命名空间 - 在您尝试运行之前,您可能不会收到编译器错误。如果重定向被删除或注释掉,错误就会消失......

BTW -- What the hell do these error messages mean? Is this MS.Access, with the "misdirection" -- ??

顺便说一句——这些错误消息到底是什么意思?这是带有“误导”的 MS.Access 吗??

DGK

DGK

回答by David

I had a similar error but not from a Conversion...

我有一个类似的错误,但不是来自转换......

System.Web.HttpException: 'Namespace.Website.MasterUserPages' is not allowed here because it does not extend class 'System.Web.UI.MasterPage'

System.Web.HttpException: 此处不允许使用“Namespace.Website.MasterUserPages”,因为它不扩展类“System.Web.UI.MasterPage”

I was also extending the MasterPage class.

我也在扩展 MasterPage 类。

The error was due to a simple compilation error in my Master Page itself:

该错误是由于我的母版页本身的一个简单的编译错误造成的:

System.Web.HttpCompileException: c:\directory\path\Website\MasterUserPages.Master(30): error CS1061: 'ASP.masteruserpages_master' does not contain a definition for 'btnHelp_Click' and no extension method 'btnHelp_Click' accepting a first argument of type 'ASP.masteruserpages_master' could be found (are you missing a using directive or an assembly reference?)

System.Web.HttpCompileException: c:\directory\path\Website\MasterUserPages.Master(30): 错误 CS1061: 'ASP.masteruserpages_master' 不包含 'btnHelp_Click' 的定义并且没有接受第一个参数的扩展方法 'btnHelp_Click'可以找到类型为“ASP.masteruserpages_master”的(您是否缺少 using 指令或程序集引用?)

I was not able to see the error until I moved the MasterPage to the root website folder. Once that was taken care of I was able to put my MasterPage back in the folder I wanted.

在我将 MasterPage 移动到根网站文件夹之前,我无法看到错误。一旦解决了这个问题,我就可以将我的 MasterPage 放回我想要的文件夹中。

回答by PeterX

My issue was simple: the Master page and Master.Designer.cs class had the correct Namespace, but the Master.cs class had the wrong namespace.

我的问题很简单:母版页和 Master.Designer.cs 类具有正确的命名空间,但 Master.cs 类具有错误的命名空间。

回答by Mike Gledhill

I had this issue, as I had copied a (fairly generic) webpage from one of my ASP.Net applications into a new application.

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

I changed the relevant namespace commands, to reflect the new location of the file... but... I had forgotten to change the Inheritsparameter in the aspx page itself.

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

<%@ Page MasterPageFile="" StylesheetTheme="" Language="C#"
    AutoEventWireup="true" CodeBehind="MikesReports.aspx.cs" 
    Inherits="MikesCompany.MikesProject.MikesReports" %>

Once I had changed the Inherits parameter, the error went away.

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

回答by iCon

You can always refractor the namespace and it will update all the pages at the same time. Highlight the namespace, right click and select refractor from the drop down menu.

你总是可以折射命名空间,它会同时更新所有页面。突出显示命名空间,右键单击并从下拉菜单中选择 refractor。

回答by surbhi

I delete that web page that i want to link with master page from web application,add new web page in project then set the master page(Initially I had copied web page from web site into Web application fro coping that aspx page (I was converting website to web application as project))

我从 Web 应用程序中删除了我想与母版页链接的网页,在项目中添加新网页,然后设置母版页(最初我已将网页从网站复制到 Web 应用程序以应对该 aspx 页面(我正在转换网站到 Web 应用程序作为项目))

回答by biggles

I had copied and renamed the page (aspx/cs). The page name was "mainpage" so the class name at the top of the cs file as follows:

我已经复制并重命名了页面(aspx/cs)。页面名称是“mainpage”,因此在 cs 文件顶部的类名称如下:

enter image description here

enter image description here

After renaming the class to match this error was resolved.

重命名类以匹配此错误后解决了。

回答by JCollerton

For me I had all of the namespaces on the pages and none of the solutions above fixed it. My problem was in:

对我来说,我拥有页面上的所有命名空间,但上述解决方案都没有修复它。我的问题是:

<%@ Page Language="C#" 
AutoEventWireup="true" 
CodeBehind="xxx.aspx.cs" 
Inherits="xxx.xxx.xxx" 
MasterPageFile="~masterurl/default.master" %>

Then in my aspx.csfile the namespace did not match the Inheritstag. So it needed

然后在我的aspx.cs文件中,命名空间与Inherits标签不匹配。所以它需要

namespace xxx.xxx.xxx

In the .csto match the Inherits.

.cs匹配Inherits.

回答by natoloefstop

Remember.. inherits is case sensitive for C# (not so for vb.net)

请记住..继承对于 C# 区分大小写(对于 vb.net 不是这样)

Found that out the hard way.

发现了困难的方式。