【问题标题】:Http Redirect 302Http 重定向 302
【发布时间】:2011-08-29 17:39:57
【问题描述】:

我正在尝试进行 HTTP 302 重定向,但在调试模式下运行时出现以下异常。

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

var response = HttpContext.Current.Response; 
response.Clear(); 
response.Status = "302 Found";
response.AddHeader("Location", "http://google.com"); 
response.End();
response.Flush();

长话短说,这个调用不会刷新响应,也不会重定向。

我怎样才能让它工作?

【问题讨论】:

标签: c# asp.net http redirect http-status-code-302


【解决方案1】:

您不应该同时调用 EndFlush 以这种方式 - 要使用 HTTP 302 进行重定向,您应该使用 HttpContext.Current.Response.Redirect 参见 http://msdn.microsoft.com/en-us/library/a8wa7sdt.aspx

【讨论】:

  • 是 302 重定向吗?这对我有用,但我不确定这是否是 302
【解决方案2】:

HttpResponse 对象具有执行 302 重定向的方法。

Response.Redirect("page.aspx") 

尽管您的代码应该可以正常工作,因为这是实现301 redirect 的常用方法。

请注意,response.Flush() 是多余的,因为响应缓冲区已刷新到客户端并且执行将在 response.End() 处结束,因此不会执行该行。

使用 similar problems 搜索其他人的 google 搜索指向此知识库文章 http://support.microsoft.com/kb/312629/EN-US/,这可能是您的问题的原因。

【讨论】:

  • 是 302 重定向吗?这对我有用,但我不确定这是否是 302
  • 是的,根据文档,你可以使用firebug或fiddler之类的东西来检查。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-08-16
  • 2013-06-20
  • 2018-07-15
  • 2019-05-10
  • 2017-03-16
  • 2013-08-15
相关资源
最近更新 更多