ni5*_*ni6 4 c# asp.net http-status-code-303
有没有人知道如何使用http状态代码303(参见其他)在ASP.NET中重定向当前请求.
代码片段非常受欢迎!
Kla*_*sen 13
你应该能够这样做:
HttpContext.Current.Response.Clear();
HttpContext.Current.Response.Status = "303 See Other";
HttpContext.Current.Response.AddHeader("Location", newLocation);
HttpContext.Current.Response.End();
Run Code Online (Sandbox Code Playgroud)
这是作为扩展方法的.NetCore/.Net5.0+实现。
public static class ControllerExtensions
{
public static ActionResult SeeOther(this Controller controller, string location)
{
controller.Response.Headers.Add("Location", location);
return new StatusCodeResult(303);
}
}
Run Code Online (Sandbox Code Playgroud)
控制器的用法: return this.SeeOther("/resource-endpoint");