【问题标题】:How to create 303 Response in asp.net如何在 asp.net 中创建 303 响应
【发布时间】:2012-02-29 10:34:28
【问题描述】:

有谁知道如何在 ASP.NET 中重定向当前请求使用 http 状态代码 303 (SeeOther)。

代码 sn-ps 非常受欢迎!

【问题讨论】:

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


    【解决方案1】:

    你应该可以这样做:

      HttpContext.Current.Response.Clear();
      HttpContext.Current.Response.Status = "303 See Other";
      HttpContext.Current.Response.AddHeader("Location", newLocation);
      HttpContext.Current.Response.End();
    

    【讨论】:

      【解决方案2】:

      这是作为扩展方法.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");

      【讨论】:

      • HTTP 303 用于“查看其他”,而不是“存在”。没有名为“Exists”的 HTTP 状态代码。
      • 考虑使用 this ControllerBase 代替,Uri location 用于强类型化,并表示意图(Uri 对象可用于表示相对 URI,而不仅仅是绝对 URI)跨度>
      • 我承认我的第二条评论是吹毛求疵,当然——但我的第一条评论是严肃的。 HTTP 303 被明确定义为“查看其他”(阅读规范!)。 HTTP 303 的语义没有说也没有暗示 Location: 目标“存在”(例如,303 重定向导致 404 是有效的)。我通过将方法命名为 Exists 而不是 SeeOther 来断言您完全不正确。
      猜你喜欢
      • 2010-09-29
      • 1970-01-01
      • 2012-12-25
      • 2018-01-20
      • 1970-01-01
      • 2016-04-10
      • 1970-01-01
      • 1970-01-01
      • 2022-10-06
      相关资源
      最近更新 更多