response.redirect在asp.net中无法正常工作 [英] response.redirect not working properly in asp.net

查看:80
本文介绍了response.redirect在asp.net中无法正常工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想根据用户的角色重定向不同页面的用户。如果用户是管理员或超级管理员,他将被重定向到AdmnDashboard.aspx,其余的用户被重新扫描到其他页面..

问题是虽然我以管理员身份登录并调试我的代码,它验证我是管理员但是当它到达response.redirect方法时,它突然转到Catch部分并给出错误消息..pllz做一些建议改变..



 受保护  void  Page_Load(对象发​​件人,EventArgs e)
{
尝试
{
if (!IsPostBack)
{

lblUserName.Text = Session [ 雇员]的ToString();
lblDate.Text = Convert.ToString(DateTime.Now.ToString( dd-MMM-yyyy< /跨度>));

string role = Session [ 作用]的ToString();
if (role == Super Admin || role == Admin
{
Response.Redirect( AdminDashboard.aspx);

}
else
{
Response.Redirect( ClientDashBoard.aspx);
}
}
}
catch (例外情况)
{
string str = ex.Message;
Response.Redirect( ErrorPage1.aspx?Error =' + str + ');
}

}

解决方案

Response.Redirect方法 [ ^ ]只用一个参数调用时会抛出 ThreadAbortException 。您应该将false作为第二个参数传递,然后完成请求。引用上面提到的链接:

MSDN HttpResponse.Redirect方法:

在页面处理程序中使用此方法时要终止对一个页面的请求并为另一个页面启动新请求,请将endResponse设置为false,然后调用CompleteRequest方法。如果为endResponse参数指定true,则此方法为原始请求调用End方法,该方法在完成时抛出ThreadAbortException异常。此异常对Web应用程序性能有不利影响,这就是建议为endResponse参数传递false的原因





这是处理它的最佳方法。您也可以在代码中专门处理ThreadAbortException。

 尝试 
{
Response.Redirect( AdminDashboard.aspx);
}
catch (ThreadAbortException te)
{
// 不做任何事情或根据需要处理
}



希望有所帮助!


Hai rahulDer,



请尝试我的解决方案并回复我的工作与否。我只是修改了你的代码,使用这个并回复,

  string  role = Session [ 角色]。ToString(); 
if (role == Super Admin || role == Admin
{
Response.Redirect( AdminDashboard.aspx false );

}
else
{
Response.Redirect( ClientDashBoard.aspx false );
}





在response.redirect函数中添加false。你可以告诉我是什么原因它去捕捉部分,为什么你写Error.aspx页面?如何在你的代码中出现异常部分?如果角色有值,它会重定向到AdminDashboard页面,如果没有角色,它会自动转到ClientDashboard页面。然后yu在异常中添加错误页面。



现在它来了exece我知道,我也尝试在我的位置,你纠正它去异常bcz一些reasoan。



注意:如果你有工作代码或任何明确你需要点击接受解决方案或在我自己解决了这个问题中发布你的工作代码!别忘了,不仅有利于其他人也有同样的问题可以快速得到解决方案。



问候

Aravind



Happy Codings .....


I want to redirect users on different pages based on their role ,i.e. if user is admin or super admin he will be redirected to AdmnDashboard.aspx nad rest of the users are redirceted to some other page..
problem is that though i am log in as admin and debug my code, it validate that i am admin but when it reaches to response.redirect method,it suddenly goes to Catch part and give error message ..pllz do suggest some changes..

protected void Page_Load(object sender, EventArgs e)
    {
        try
        {
            if (!IsPostBack)
            {

                lblUserName.Text = Session["Employee"].ToString();
                lblDate.Text = Convert.ToString(DateTime.Now.ToString("dd-MMM-yyyy"));

                string role = Session["Role"].ToString();
                if (role == "Super Admin" || role == "Admin")
                {
                    Response.Redirect("AdminDashboard.aspx");

                }
                else
                {
                    Response.Redirect("ClientDashBoard.aspx");
                }
            }
        }
        catch (Exception ex)
        {
            string str = ex.Message;
            Response.Redirect("ErrorPage1.aspx?Error='" + str + "'");
        }

    }

解决方案

Response.Redirect Method[^] when called with just one parameter would throw ThreadAbortException. You should pass false as the second parameter and then complete the request. Quoting from the link mentioned above:

MSDN HttpResponse.Redirect Method :

When you use this method in a page handler to terminate a request for one page and start a new request for another page, set endResponse to false and then call the CompleteRequest method. If you specify true for the endResponse parameter, this method calls the End method for the original request, which throws a ThreadAbortException exception when it completes. This exception has a detrimental effect on Web application performance, which is why passing false for the endResponse parameter is recommended



That's the best way to handle it. You may also handle ThreadAbortException specifically in your code.

try
{
    Response.Redirect("AdminDashboard.aspx");
}
catch (ThreadAbortException te)
{
    //do nothing or handle as required
}


Hope that helps!


Hai rahulDer,

Pls try my solution and reply me it work or not.i am just modified ur code ,use this and reply,

string role = Session["Role"].ToString();
              if (role == "Super Admin" || role == "Admin")
              {
                  Response.Redirect("AdminDashboard.aspx",false);

              }
              else
              {
                  Response.Redirect("ClientDashBoard.aspx",false);
              }



add "false" in response.redirect function.And can u tell me in what reason it goes to catch part,why u write Error.aspx page ? How it go exception part in ur code ? if role have values it redirect to AdminDashboard page and if not vlaue in role it automatically go to ClientDashboard page.Then y u add error page in exception.

Now it come to exeception i know and i am also try in my place,u r correct it goes to exception bcz some reasoan.

Note: if u got working code or any clear u need to click "Accept Solution" or post ur working code in "I've solved this myself!" dont forget,not only u benefit other people also have same problem can get solution quickly.

Regards
Aravind

Happy Codings.....


这篇关于response.redirect在asp.net中无法正常工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆