【发布时间】:2012-02-29 10:34:28
【问题描述】:
有谁知道如何在 ASP.NET 中重定向当前请求使用 http 状态代码 303 (SeeOther)。
代码 sn-ps 非常受欢迎!
【问题讨论】:
标签: c# asp.net http-status-code-303
有谁知道如何在 ASP.NET 中重定向当前请求使用 http 状态代码 303 (SeeOther)。
代码 sn-ps 非常受欢迎!
【问题讨论】:
标签: c# asp.net http-status-code-303
你应该可以这样做:
HttpContext.Current.Response.Clear();
HttpContext.Current.Response.Status = "303 See Other";
HttpContext.Current.Response.AddHeader("Location", newLocation);
HttpContext.Current.Response.End();
【讨论】:
这是作为扩展方法的.NetCore实现。
public static class ControllerExtensions
{
public static ActionResult SeeOther(this Controller controller, string location)
{
controller.Response.Headers.Add("Location", location);
return new StatusCodeResult(303);
}
}
控制器使用: return this.SeeOther("/resource-endpoint");
【讨论】:
this ControllerBase 代替,Uri location 用于强类型化,并表示意图(Uri 对象可用于表示相对 URI,而不仅仅是绝对 URI)跨度>
Location: 目标“存在”(例如,303 重定向导致 404 是有效的)。我通过将方法命名为 Exists 而不是 SeeOther 来断言您完全不正确。