Http Redirect 302

Dar*_*der 8 c# asp.net redirect http http-status-code-302

我正在尝试进行HTTP 302重定向,但在调试模式下运行时遇到以下异常.

Unable to evaluate expression because the code is optimized or a native frame is on top of the call stack
Run Code Online (Sandbox Code Playgroud)
var response = HttpContext.Current.Response; 
response.Clear(); 
response.Status = "302 Found";
response.AddHeader("Location", "http://google.com"); 
response.End();
response.Flush();
Run Code Online (Sandbox Code Playgroud)

长话短说,这个电话不是冲洗响应而不是重定向.

我怎样才能使这个工作?

Yah*_*hia 9

你不应该呼吁双方EndFlush以这种方式-与HTTP重定向302,你应该使用HttpContext.Current.Response.Redirecthttp://msdn.microsoft.com/en-us/library/a8wa7sdt.aspx


Chr*_*ver 6

HttpResponse对象具有用于执行302重定向的方法.

Response.Redirect("page.aspx") 
Run Code Online (Sandbox Code Playgroud)

虽然您的代码应该可以正常工作,因为这是实现a的常用方法301 redirect.

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

谷歌搜索其他有类似问题的人指向这篇知识库文章http://support.microsoft.com/kb/312629/EN-US/,这可能是导致您出现问题的原因.