C# 如何连接.cs类和.aspx类?

C# 如何连接.cs类和.aspx类?,c#,asp.net,C#,Asp.net,我试图在我的.aspx类中调用我的.cs类中的funktions,它似乎找到了我的类,但在我运行应用程序时给了我一个“应用程序“/”中的服务器错误。它说这是我的公共部分类Create_b_代码就是问题所在 我尝试添加reference System.Web.dll,但它似乎没有任何作用。我还尝试添加:System.Web.UI.Page,但它似乎可以找到“UI”部分,我无法安装它 这是我的.cs类我的功能创建了一个txt文件,如果我只运行这个类,它就会工作 using MySql.Data.My

我试图在我的.aspx类中调用我的.cs类中的funktions,它似乎找到了我的类,但在我运行应用程序时给了我一个“应用程序“/”中的服务器错误。它说这是我的公共部分类Create_b_代码就是问题所在

我尝试添加reference System.Web.dll,但它似乎没有任何作用。我还尝试添加:System.Web.UI.Page,但它似乎可以找到“UI”部分,我无法安装它

这是我的.cs类我的功能创建了一个txt文件,如果我只运行这个类,它就会工作

using MySql.Data.MySqlClient;
using System;
using System.IO;

public partial class Create_b_code
{
  public static void Create_new_file(string LTOname)
    {
        string path = @"C:\test\logfile_timeout_" + LTOname +".txt";

        if (!File.Exists(path))
        {
            // Create a file to write to.
            using (StreamWriter sw = File.CreateText(path))
            {
                sw.WriteLine("  ");                
            }
        }
    }   
}
这是我的.aspx类,如果我不调用该部分,所有操作都有效


在.cs文件中尝试以下操作:

public partial class Create_b_code : System.Web.UI.Page

请注意列出的代码文件的名称:

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Create_b_code.cs"
Inherits="Create_b_code" %>
确保这实际上是代码隐藏文件的名称


您可以有单独的、独立的代码文件,但其工作方式与web表单+代码隐藏文件不同。

正如错误所述:
public partial class Create\u b\u code:Page或UserControl
当我使用“public partial class Create\u b\u code:Page或UserControl”时,它导入参考System.web.dll并使用“using System.web.UI”但我得到一个错误,它不适用于我的项目。我可以创建一个支持引用的新项目,但不知道哪些项目支持引用?代码文件的全名是什么?我尝试在类名后键入此项,但它说“CS0234 C#命名空间“System.Web”中不存在类型或命名空间名称“UI”(是否缺少程序集引用?)”我尝试添加引用“System.Web.dll”但它不起作用,我不知道它是否是错误的引用或什么。我搜索了很多次,都说是那个。可能是那里的项目不支持引用吗?我尝试过用相同的代码制作两个不同的项目。一个是“Windows应用程序(.NET Fameowrk)”,另一个是Windows Froms应用程序(.NET Core)?在Visual Studio中,选择文件>新建项目。在左侧,展开Visual C#,选择Web,然后选择ASP.NET Web表单应用程序。还可以添加Roman K回答中提到的“:System.Web.UI.Page”。感谢您的帮助,在跟踪您和Roman K之后,它现在可以工作了。我不能。当我投票时,它会说“谢谢您的反馈!声誉低于15的人所投的票将被记录,但不会改变公开显示的帖子分数。”
Server error in application '/'.
Compile
Description: An error occurred while compiling a resource required to process this
request. Read the following details about the error and make the necessary changes
to the source code.
Compilation function error message: ASPNET: Verify that the class defined in this 
code file matches the 'inherits' attribute and extends the correct base class (for 
example, Page or UserControl).
Source Error:
Linje 7:  public partial class Create_b_code
public partial class Create_b_code : System.Web.UI.Page
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Create_b_code.cs"
Inherits="Create_b_code" %>
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Create_b_code.aspx.cs"
Inherits="Create_b_code" %>