简体   繁体   English

ASP.NET - 在控制器响应中设置状态代码

[英]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.

相关问题 从角度6的asp.net核心读取响应状态码 - Read status code on response from asp.net core in angular 6 Asp.net Web Api 将响应状态代码设置为数字 - Asp.net Web Api set response status code to number 在 graphql controller 响应上设置状态码 - setting status code on graphql controller response ASP.NET Core 3.1 中 API Controller 的单元测试返回错误的状态代码 - Unit test for API Controller in ASP.NET Core 3.1 returning a wrong status code ASP.NET控制器操作返回状态代码200,但EndRequest将其作为204(NoContent) - ASP.NET controller action returns status code 200 but EndRequest has it as 204 (NoContent) 更新ajax ASP.NET MVC时收到500状态代码响应 - I'm getting a 500 status code response when I update ajax ASP.NET MVC 如何在ASP.Net中发送状态代码500并仍然写入响应? - How to send a Status Code 500 in ASP.Net and still write to the response? ASP.NET OpenIdConnectAuthenticationOptions - MVC 响应状态码不表示成功:400(错误请求) - ASP.NET OpenIdConnectAuthenticationOptions - MVC Response status code does not indicate success: 400 (Bad Request) C# 发布到 asp.net webapi Apiexception“响应的 HTTP 状态代码未被排除 (201)。” - C# post to asp.net webapi Apiexception "The HTTP status code of the response was not excepted (201)." 状态码 406 使用 ASP.NET 内核中的格式响应数据 Web API - Status code 406 using Format response data in ASP.NET Core Web API
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM