Response.Redirect() 不起作用

发布于 2024-11-29 08:26:56 字数 802 浏览 0 评论 0原文

我有 Default.aspx 页面,它继承自 BasePage.cs,而 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;
            }
        }
    }
}

这是我的 Basepage HttpContext.Current.Response.Redirect("Main.aspx", true);

我希望重定向停止执行 BasePage 并立即跳出。问题是,事实并非如此。

当我在调试模式下运行时,它会继续单步执行,就好像它不只是重定向并离开一样。 如何安全地重定向?

I have Default.aspx page, which inherits from BasePage.cs, which inherits from System.Web.UI.Page. BasePage is where I check if the session has timed out. When the session has timed out and the user clicks on something, I need to redirect the user back to the "Main.aspx" page.

Here is the code in my Basepage

 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);

I want the redirect to stop execution of BasePage and immediately jump out. The problem is, it doesn't.

When I run in debug mode, it contines stepping through as though it didn't just redirect and leave.
How can I safely redirect?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

离鸿 2024-12-06 08:26:56

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

编辑:添加了围绕response.redirect的页面检查

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

Seeing that your base class is inheriting from System.Web.UI.Page, you don't need to use HttpContext. Try it without and see if it helps.

EDIT: Added page check around response.redirect

if (!Request.Url.AbsolutePath.ToLower().Contains("main.aspx"))
{
    Response.Redirect("<URL>", false);
    HttpContext.Current.ApplicationInstance.CompleteRequest();
}
南汐寒笙箫 2024-12-06 08:26:56

我不认为这正是您正在寻找的,但也许这会起作用:

Server.Transfer("<URL>")

I don't think it's exactly what you are looking for but maybe this would work:

Server.Transfer("<URL>")
小帐篷 2024-12-06 08:26:56

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

 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 之外的其他东西的想法。

I struggled with the same problem but on Asp.Net MVC 3.0.
Response.Redirect simply did not work, so I found simple way to use RedirectToAction method that can be inherit from 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);
            }
        }

    }
}

This works fine for Asp.Net MVC, but that might give an idea of using something else than Response.Redirect.

像你 2024-12-06 08:26:56

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

HttpContext.Current.ClearError();
HttpContext.Current.Response.Redirect("您的目标网址", false);

some time your page has an error and you not see it
please check this code below

HttpContext.Current.ClearError();
HttpContext.Current.Response.Redirect("your destination url", false);

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文