在响应中返回HttpStatusCode [英] Return HttpStatusCode in the Response

查看:232
本文介绍了在响应中返回HttpStatusCode的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有一种简单的方法可以为我的api返回一个HttpStatusCode?我找到了一种更详细的方法

Is there a easy way to return just a HttpStatusCode for my api? I've found a slightly more verbose way to do this

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

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

I've taken a look at the Response class's source code and I see

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

这是我要找的东西吗?如果是这样,我将如何使用它,

Is this what I'm looking for? If so how do I use it, what I'd really like would be able to do is

return Response(HttpStatusCode.NoContent);

推荐答案

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

You can just directly return a HttpStatusCode from your action:

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

来自文档:

Response对象声明了几个隐式强制转换运算符,这些运算符 使操作也可以返回以下任何内容而不是Response对象: 以下

The Response object declares several implicit cast operators which enables an action to also return, instead of a Response object, any of the following

  1. int,它将被解释为响应的HTTP状态代码
  2. HttpStatusCode可枚举值
  3. string,它将被解释为响应的正文
  4. Action<Stream>是写入响应流的函数
  1. int which will be interpreted as a HTTP status code of the response
  2. HttpStatusCode enumerable value
  3. string which will be interpreted as the body of the response
  4. Action<Stream> which is a function that writes to the response stream

这篇关于在响应中返回HttpStatusCode的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆