进行HTTP 307重定向

时间:2020-08-25 13:58:33

标签: c# asp.net redirect

出于技术原因,我们需要执行HTTP 307重定向而不是302。

我想做302:

HttpContext.Current.Response.Redirect(url)

我要做301:

HttpContext.Current.Response.RedirectPermanent(url)

我将如何实施307?

2 个答案:

答案 0 :(得分:0)

要在页面中引起307,而不是Response.Redirect,请使用Server.Transfer:-

Server.Transfer("targetpage.aspx");

然后在目标页面的Page_Load中,添加以下内容(笑脸可选):-

Response.StatusCode = 307;
Response.StatusDescription = "Temporary Redirect :)";

在浏览器的调试工具中查看网络跟踪时,看起来源页面返回307。Server.Transfer不会向浏览器发送响应,而是会立即将控制权转移到指定页面。

答案 1 :(得分:0)

通过添加位置标头并手动执行重定向来解决此问题:

                    HttpContext.Current.Response.StatusCode = 307;
                    HttpContext.Current.Response.StatusDescription = "Temporary Redirect";
                    HttpContext.Current.Response.AddHeader("Location", redirectURL);
                    HttpContext.Current.Response.End();