【问题标题】:C# Class Inheritance IssueC# 类继承问题
【发布时间】:2012-05-11 06:33:04
【问题描述】:

所以我没有使用母版页,因为项目规模较小,而且总体上是为了保持精简。因此,我想在一个简单的 cs 文件中包含一个类并在同一页面上使用它。

// <copyright file="anon.cs" company="Anonymous">
//     ;
// </copyright>

namespace cheese.pies.org
{
using System;
using System.IO;
using System.Net;
using System.Web;
using System.Xml;

/// <summary>
///   Class to process CAS authentication.
///   Based on solutions from CASE and Yale.
/// </summary>
public class CasLogin
{
    /// <summary>
    ///   Address of university cas host
    /// </summary>
    private const string CasHost = "notimportant";

    /// <summary>
    ///   Get the logged-in user's id or redirect them to login.
    /// </summary>
    /// <returns>
    /// DirectoryID of logged in user
    /// </returns>
    public static string GetId()
    {
        ...
    }
    ...
}

其余的相当不重要。我认为这是一个基本的语法错误。

这里是文件test.aspx,它使用了一个页面包含。

<% @Page Language="C#" Inherits="CasLogin" CodeFile="anon.cs" %>
<script language="C#" runat="server">
protected void Page_Load(object sender, EventArgs e) {
    String directoryId = CasLogin.GetId();
    FormsAuthentication.RedirectFromLoginPage(directoryId, false);
}
</script>

我得到的错误是:

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).

我对这种事情还很陌生,我想知道出了什么问题以及正确的语法是什么。谢谢!

【问题讨论】:

  • 您能否完全限定类型名称,然后再试一次?例如cheese.pies.org.CasLogin.GetId - 听起来可能只需要一个导入指令。
  • 你的错误是试图和你说话 :)
  • 如果你在为 Anonymous 工作,你会认为你可以自己解决这个问题...

标签: c# asp.net class inheritance methods


【解决方案1】:

只需更新类以从页面派生:

public class CasLogin : Page
{

【讨论】:

    【解决方案2】:

    您的错误消息告诉您问题所在。将类更改为:

    public class CasLogin : Page
    

    【讨论】:

      【解决方案3】:

      它准确地告诉你出了什么问题:

      编译器错误消息:ASPNET:确保此代码文件中定义的类与“继承”属性匹配,并且它扩展了正确的基类(例如 Page 或 UserControl)。

      public class CasLogin {} 是否扩展 PageUserControl

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-05-11
        • 2011-09-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多