简体   繁体   中英

How to return status code if action return type is custom class in API?

I have action that returns my custom class MyClass :

[HttpGet]
public async Task<MyClass> Get([FromUri] int id)
{
   // return 403 ???
}

Is it possible to return response with 403 status code? Or only way for it to return ActionResult ? Don't want to rewrite action with other return type.

(This is.Net Core) Due to the signature, you'd be unable to do this. You should change your signature to:

[HttpGet]
public async Task<IActionResult> Get([FromUri] int id)
{
    return StatusCode(403);
}

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