[英]ASP.NET - setting status code in controller response
I am a little bit confused sending specific status codes in responses from Web API controllers. 我有点困惑在Web API控制器的响应中发送特定的状态代码。 Here is example of a simple controller method: 这是一个简单的控制器方法的例子:
public IHttpActionResult GetDeliveryMethods()
{
if (AuthenticationControl)
{
return Ok(db.DeliveryMethods);
}
else
return StatusCode(HttpStatusCode.Unauthorized);
}
Well, "AuthenticationControl" represents some procedure checking, if a client can get data from the server. 好吧,如果客户端可以从服务器获取数据,“AuthenticationControl”表示一些过程检查。 Whole method works well, but when I get back a response from the server status code is always 200 OK. 整个方法运行良好,但当我从服务器状态代码返回响应总是200 OK。 I don't understand why, this declaration of responses and status codes is also in main controller template in VS. 我不明白为什么,这个响应和状态代码的声明也在VS的主控制器模板中。
What I have to do to send my declaration of status code in response? 我必须做什么来发送我的状态代码声明作为回应?
Return HTTPResoponse instead of your current return type. 返回HTTPResoponse而不是当前的返回类型。
This allows you to set HTTP status codes 这允许您设置HTTP状态代码
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.