解析器错误:“_Default”此处不允许,因为它不扩展类“System.Web.UI.Page” &主类型声明
我最近在 Visual Studio 2008 中将网站项目转换为 Web 应用程序项目。我终于编译了它,并且第一页(登录屏幕)正常显示,但是当它重定向到 Default.aspx< /code> 页面时,我收到一个错误:
Parser Error Message: 'SOME.NAMESPACE.MyApplicationName.WebApplication._Default' is not allowed here because it does not extend class 'System.Web.UI.Page'.
我的所有页面都继承自一个名为“BasePage”的类,该类扩展了 System.Web.UI.Page
。显然,问题不在于该类,因为 login.aspx
页面显示时没有错误,并且它也是从该基页面继承的。
网站上的所有页面(包括登录页面)都是母版页的子级。
经过一些测试后,我确定了导致错误的原因(尽管我不知道为什么会这样做)。
在我具有以下标记的所有页面上,不会发生错误。
<%@ MasterType VirtualPath="~/MasterPages/MainMaster.master" %>
在所有不包含该行的页面上,都会发生错误确实。这贯穿于整个应用程序。我仅在需要引用母版页上控件的页面上使用该标记。
所以,我想我只需将该行添加到我的所有页面中即可完成。但是当我添加该行时,出现编译错误: “object”不包含“Master”的定义
此错误来自与我添加了“MasterType”声明的 ASPX
页面关联的 designer.cs
文件到。
我已经强制重建了设计器文件,但这并没有改变任何东西。我比较了 login.aspx
(工作)和 default.aspx
(不工作)之间设计器文件中主参考的内容,但它们完全相同。
因为我真的想让它工作,而不必在每个页面添加“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>
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");
}
}
}
这是以下的代码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>
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)
{
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(11)
我想通了。
问题是项目中仍有一些页面尚未转换为使用 Web 应用程序项目中所需的“命名空间”。
我想我认为如果仍然存在任何这些页面,它就不会编译,但是如果该页面没有引用外部的任何内容,它似乎不会发出嘎嘎声。因此,当它说它没有继承自“System.Web.UI.Page”时,那是因为它在运行时实际上无法找到类“BasePage”,因为页面本身不在 WebApplication 命名空间中。< br>
我一页一页地浏览了所有页面,并确保它们已正确添加到 WebApplication 命名空间中,现在它不仅可以毫无问题地编译,而且还可以正常显示。
耶!
从网站转换为 Web 应用程序项目是多么一次尝试啊!
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!
我遇到了这个问题,因为我将一个(相当通用的)网页从我的 ASP.Net 应用程序复制到了一个新应用程序中。
我更改了相关的命名空间命令,以反映文件的新位置...但是...我忘记更改 aspx 页面本身中的 Inherits 参数。
一旦我更改了 Inherits 参数,错误就消失了。
I had this issue, as I had copied a (fairly generic) webpage from one of my ASP.Net applications into a new application.
I changed the relevant namespace commands, to reflect the new location of the file... but... I had forgotten to change the Inherits parameter in the aspx page itself.
Once I had changed the Inherits parameter, the error went away.
查找以下内容:
项目中的任何页面缺少或不同的命名空间...
如果项目中的任何页面具有> 或与
默认命名空间不同的命名空间。 aspx,你会得到这个
或这个:
“Default.aspx 不属于这里”。
另外:
如果您有重定向到解决方案/项目中的页面并且
要重定向到的页面有一个错误的名称空间 - 你可能无法得到
编译器错误,直到您尝试运行。
如果重定向被删除
或注释掉,错误消失......
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 aDIFFERENT Namespace than Default.aspx, you will get this
or this:
"Default.aspx does not belong here".
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...
我复制并重命名了该页面(aspx/cs)。页面名称为“mainpage”,因此 cs 文件顶部的类名称如下:
重命名该类以匹配此错误后已解决。
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:
After renaming the class to match this error was resolved.
对我来说,我在页面上拥有所有名称空间,并且上面的解决方案都没有修复它。我的问题在于:
然后在我的
aspx.cs
文件中,命名空间与Inherits
标记不匹配。因此它需要在
.cs
中来匹配Inherits
。For me I had all of the namespaces on the pages and none of the solutions above fixed it. My problem was in:
Then in my
aspx.cs
file the namespace did not match theInherits
tag. So it neededIn the
.cs
to match theInherits
.我有一个类似的错误,但不是来自转换...
System.Web.HttpException: 'Namespace.Website.MasterUserPages' 在这里不允许,因为它不扩展类 'System.Web.UI.MasterPage'
我还扩展了母版页类。
该错误是由于我的母版页本身的一个简单编译错误造成的:
System.Web.HttpCompileException: c:\directory\path\Website\MasterUserPages.Master(30): error CS1061: 'ASP.masteruserpages_master' does not contains aDefinition对于“btnHelp_Click”,并且找不到接受“ASP.masteruserpages_master”类型的第一个参数的扩展方法“btnHelp_Click”(您是否缺少 using 指令或程序集引用?)
直到我移动了MasterPage 到网站根文件夹。一旦解决了这个问题,我就可以将我的 MasterPage 放回到我想要的文件夹中。
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'
I was also extending the MasterPage class.
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?)
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.
我的问题很简单:母版页和 Master.Designer.cs 类具有正确的命名空间,但 Master.cs 类具有错误的命名空间。
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.
您始终可以折射命名空间,它将同时更新所有页面。突出显示命名空间,右键单击并从下拉菜单中选择 refractor。
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.
我删除了想要从Web应用程序中与母版页链接的网页,在项目中添加新网页,然后设置母版页(最初我已将网页从网站复制到Web应用程序中以应对该aspx页面(我正在转换网站到 Web 应用程序作为项目))
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))
请记住..继承对于 C# 是区分大小写的(对于 vb.net 则不是这样)
发现这一点很困难。
Remember.. inherits is case sensitive for C# (not so for vb.net)
Found that out the hard way.
对我来说,解决方法是不同的。相应的aspx页面已从项目中卸载,我将其添加回来,错误消失了。
For me the fix was different. The corresponding aspx page has been unloaded from the project, I added that back and the error is gone.