【问题标题】:Return HttpStatusCode in the Response在响应中返回 HttpStatusCode
【发布时间】:2013-10-19 23:59:41
【问题描述】:

有没有一种简单的方法可以只为我的 api 返回一个 HttpStatusCode?我找到了一种稍微冗长的方法来做到这一点

return Response.AsJson(new object(), HttpStatusCode.NoContent);

我查看了Response 类的源代码,我看到了

 public static implicit operator Response(HttpStatusCode statusCode) { ... }

这就是我要找的吗?如果是这样我该如何使用它,我真正想要的是能够做的是

return Response(HttpStatusCode.NoContent);

【问题讨论】:

    标签: nancy


    【解决方案1】:

    您可以直接从您的操作中返回 HttpStatusCode

    Get["/hello/"] = parameters => {
        return HttpStatusCode.NoContent;
    };
    

    来自documentation

    Response 对象声明了几个隐式转换运算符,它们 使动作也可以返回,而不是 Response 对象,任何 以下

    1. int 将被解释为响应的 HTTP 状态代码
    2. HttpStatusCode可枚举值
    3. string 将被解释为响应的正文
    4. Action<Stream> 这是一个写入响应流的函数

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2023-02-17
      • 2017-10-07
      • 2015-03-30
      • 1970-01-01
      • 2013-08-15
      • 2017-10-09
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多