简体   繁体   中英

ASP.NET - setting status code in controller response

I am a little bit confused sending specific status codes in responses from Web API controllers. 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. Whole method works well, but when I get back a response from the server status code is always 200 OK. I don't understand why, this declaration of responses and status codes is also in main controller template in VS.

What I have to do to send my declaration of status code in response?

Return HTTPResoponse instead of your current return type.

This allows you to set HTTP status codes

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM