Response.Redirect() 不起作用
我有 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
看到您的基类继承自System.Web.UI.Page,您不需要使用HttpContext。尝试不使用它,看看是否有帮助。
编辑:添加了围绕response.redirect的页面检查
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
我不认为这正是您正在寻找的,但也许这会起作用:
I don't think it's exactly what you are looking for but maybe this would work:
我在 Asp.Net MVC 3.0 上遇到了同样的问题。
Response.Redirect 根本不起作用,所以我找到了使用可以从 Controller 继承的 RedirectToAction 方法的简单方法。
这对于 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.
This works fine for Asp.Net MVC, but that might give an idea of using something else than Response.Redirect.
有时您的页面出现错误,但您看不到它
请检查下面的代码
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);