如何从我的自定义 c# 类调用 response.redirect

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19612462/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 15:25:09  来源:igfitidea点击:

how to call response.redirect from my custom c# class

c#asp.netresponse.redirect

提问by Onat Tanriover

I am in the process of coding a web application with asp.net. The users enter their credentials and these are validated against the actual email address and password for authorization. I wrote some classes for processing these data (in a seperate .cs file).

我正在使用 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.
    }

}

However I seem not to be able to access the Response.Redirect() method from inside the Login class which is in the seperate cs file. (I can access it from inside the event handlers I wrote for the aspx page.) Does anyone have an idea? Thank you in advance for helping!

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

回答by Jim W says reinstate Monica

Yes, because your method there is static (it would work in the code-behind if it wasn't static), so you'll want to pass response to it, like this:

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

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

for convention Response should be changed to 'response'.

对于约定响应应更改为“响应”。

回答by IrishChieftain

Try using the full namespace:

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

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

回答by Ali Gh

first level :

第一级 :

using System.Web;

in next use this in your code :

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

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

hope helps you

希望能帮到你