Response.Redirect()不起作用

问题描述 投票:4回答:4

我有Default.aspx页面,它继承自BasePage.cs,它继承自System.Web.UI.Page。 BasePage是我检查会话是否超时的地方。当会话超时并且用户点击某些内容时,我需要将用户重定向回“Main.aspx”页面。

这是我的基页中的代码

 override protected void OnInit(EventArgs e)
{
  base.OnInit(e);
  if (Context.Session != null)
    {
        if (Session.IsNewSession)
        {
            string cookie = Request.Headers["Cookie"];
            if ((null != cookie) && (cookie.IndexOf("ASP.NET_SessionId") >= 0))
            {
                HttpContext.Current.Response.Redirect("Main.aspx", true);
                return;
            }
        }
    }
}

HttpContext.Current.Response.Redirect(“Main.aspx”,true);

我希望重定向停止执行BasePage并立即跳出。问题是,它没有。

当我在调试模式下运行时,它会逐步调整,就好像它不仅仅是重定向和离开一样。我怎样才能安全地重定向?

asp.net session response.redirect
4个回答
4
投票

看到您的基类继承自System.Web.UI.Page,您不需要使用HttpContext。不用尝试,看看是否有帮助。

编辑:在response.redirect周围添加了页面检查

if (!Request.Url.AbsolutePath.ToLower().Contains("main.aspx"))
{
    Response.Redirect("<URL>", false);
    HttpContext.Current.ApplicationInstance.CompleteRequest();
}

1
投票

我不认为这正是你想要的,但也许这会奏效:

Server.Transfer("<URL>")

1
投票

我在Asp.Net MVC 3.0上遇到了同样的问题。 Response.Redirect根本不起作用,所以我找到了使用RedirectToAction方法的简单方法,可以从Controller继承。

 public class SessionExpireFilter : ActionFilterAttribute
{
    public override void OnActionExecuting(ActionExecutingContext filterContext)
    {
        HttpContext context = HttpContext.Current;

        if (context.Session != null) // check if session is supported
        {
            if (context.Session.IsNewSession) // if it says it is a new session, but exisitng cookie exists that means session expired
            {
                string sessionCookie = context.Request.Headers["Cookie"];

                if ((sessionCookie != null) && (sessionCookie.IndexOf("ASP.NET_SessionId") >= 0))
                {
                    string redirectTo = "~/Account/Expired";
                    filterContext.Result = new RedirectResult(redirectTo);


                }
            }
            else
            {
                base.OnActionExecuting(filterContext);
            }
        }

    }
}

这适用于Asp.Net MVC,但这可能会让人想到使用除Response.Redirect之外的其他东西。


0
投票

有时您的页面出现错误而您看不到它请检查下面的代码

HttpContext.Current.ClearError(); HttpContext.Current.Response.Redirect(“你的目标网址”,false);

© www.soinside.com 2019 - 2024. All rights reserved.