首页 > 解决方案 > 尝试在浏览器中预览 ASPX 文件时出现解析器错误和/或编译器错误

问题描述

我目前正在学习 ASP,我正在努力解决与母版页继承有关的问题,它阻止了我在浏览器中预览页面,VS 2017 的设计视图将我限制为静态预览。我不是要你写整件事,但我无法掌握我在哪里犯了错误,或者我遵循的说明是否有错误。当我将其保留为 CodeBehind 时,我得到:

"Parser Error: BasePage' is not allowed here because it does not extend class 'System.Web.UI.Page" 

如果我将它更改为 CodeFile 我会得到这个:

"Compiler Error Message: ASPNET: Make sure that the class defined in this code file matches the 'inherits' attribute, and that it extends the correct base class (e.g. Page or UserControl)."

这是 Default.aspx 最重要的东西

<%@ Page Title="Home Page" Language="C#" MasterPageFile="~/MasterPages/Frontend.Master" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="Default.BasePage" %>

Default.aspx.cs 看起来像这样:

   namespace website05 
    {
public partial class Default : BasePage
{
    protected void Page_Load(object sender, EventArgs e)
    {
    }
 {
{

BasePage.cs 文件也是这样的:

   namespace website05
{
    public class BasePage : System.Web.UI.Page
{

    private void Page_PreRender(object sender, EventArgs e)
    {
        if (string.IsNullOrEmpty(this.Title) || this.Title.Equals("Untitled Page", StringComparison.CurrentCultureIgnoreCase))
        {
            throw new Exception("Page title cannot be \"Untitled Page\" or an empty string.");
        }
    }


    public BasePage()
    {
        this.PreRender += Page_PreRender;
    }
}
}

我为糟糕的格式道歉,我在这里看到过类似的问题,但答案没有产生任何结果,我对此也很陌生,所以我很确定我可能误解了以前的答案。

标签: asp.netmaster-pages

解决方案


将带有命名空间的页面放在 .aspx 中的 inherts-property 中,如 Inherits="website05.Default",此处不要使用 BasePage。


推荐阅读