如何从我的自定义 C# 类调用响应.重定向

本文关键字:调用 响应 重定向 自定义 我的 | 更新日期: 2023-09-27 18:36:56

我正在用 asp.net 编写一个Web应用程序。用户输入其凭据,并根据实际电子邮件地址和密码进行验证以进行授权。我编写了一些用于处理这些数据的类(在单独的.cs文件中)。

public static class Login
{
    public static void LoginFirstTime(string Email, string Password)
    {
      //This method logs the user in for the first time after he has registered.
       Response.Redirect("UserPage.aspx", true);
      //After logging in, the user will be redirected to his personal page.
    }
}

但是,我似乎无法从单独的 cs 文件中的 Login 类内部访问 Response.Redirect() 方法。(我可以从我为 aspx 页编写的事件处理程序中访问它。有人有想法吗?提前感谢您的帮助!

如何从我的自定义 C# 类调用响应.重定向

尝试使用完整的命名空间:

public static void LoginFirstTime(string Email, string Password)
{
    //This method logs the user in for the first time after he has registered.
    HttpContext.Current.Response.Redirect("UserPage.aspx", true);
    //After logging in, the user will be redirected to his personal page.
}

第一级:

using System.Web;

接下来在你的代码中使用它:

 HttpContext.Current.Response.Redirect("UserPage.aspx");

希望对你有帮助

是的

,因为你的方法是静态的(如果它不是静态的,它会在代码隐藏中工作),所以你需要传递响应,如下所示:

public static void LoginFirstTime(string Email, 
                                  string Password, 
                                  HttpResponse Response)
{

对于约定,响应应更改为"响应"。