删除会话或session.abandon后,重定向请求未完成

时间:2011-05-27 04:40:53

标签: asp.net session response.redirect

我在Session.Abandon之后重定向时面临一个特殊问题,这里是代码片段

if (Session["Login"] != null)
            {
                login = (LoginState)Session["Login"];
                if (!RoleValidation.ManagerRoleValidate(login.role_id))
                {
                    Session.Remove("Login");
                    Session.RemoveAll();
                    Session.Abandon();                       
                    Response.Redirect("~/Login.aspx");

                }
            }

这里如果条件为真,重定向需要很长时间......实际上它根本没有重定向到页面,就好像它似乎卡在某处...已将此部分放入try catch块,它表示为

Unable to evaluate expression because the code is optimized or a native frame is on top of the call stack

我不确定是什么导致了它。它发生的原因是什么?

完整的代码段是:

     protected void Page_Load(object sender, EventArgs e)
    {

        if (Session["Login"] != null)
        {
            login = (LoginState)Session["Login"];
            if (!RoleValidation.ManagerRoleValidate(login.role_id))
            {
                Session.Remove("Login");
                Session.RemoveAll();
                Session.Abandon();
                Response.Redirect("~/Login.aspx", false);

            }
        }
        else
        {
            Response.Redirect("~/Login.aspx");
        }


    }
public static Boolean ManagerRoleValidate(String Role_id) 
{
    if (Role_id.Equals("1") || Role_id.Equals("2") || Role_id.Equals("4") || Role_id.Equals("999"))
    {
        return true;
    }
    else
    {
        return false;

    }
}

并且Login.aspx中没有重定向,因此它可以反复重定向。

3 个答案:

答案 0 :(得分:0)

此过程中还有另一个请求,您需要另一个请求。如果您添加false it will terminate the current request并转到登录页面。

Response.Redirect("~/Login.aspx", false);

如果您在Response.Redirect下添加Try Catch Block语句,则最常出现此问题。

try
    {
        Response.Redirect(""~/Login.aspx"); // here you want to redirect, but try catch is still have to complete, It will go in the Exception block
    }
    catch (Exception ex)
    {
        throw;
    }

答案 1 :(得分:0)

可能原因可能来自Login.aspx,你的这部分代码正在执行,所以这个响应会一次又一次地重定向到Login.aspx。

答案 2 :(得分:0)

不确定为什么会造成麻烦。我已经取代了

  

session.abandon

  

session.clear

清除所有会话的方法。虽然在global.asax我写过

void Session_End(object sender, EventArgs e) 
{
    if (Request.Cookies["ASP.NET_SessionId"] != null)
    {
        Response.Cookies["ASP.NET_SessionId"].Expires = DateTime.Now.AddYears(-30);
    }

    Response.Redirect("~/Login.aspx"); 
}
删除行后

Response.Redirect("~/Login.aspx"); 

从上面的代码中它应该在global.asax中引发事件session_end但是由于某种原因需要很长时间来强制请求。这就是我如何解决我的问题。如果有人可以请解释这里有什么好处,以及上面的事情是如何解决的。请回复,以便其他人可以从中受益

相关问题