简体   繁体   English

如果操作返回类型是 API 中的自定义 class,如何返回状态码?

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

I have action that returns my custom class MyClass :我有返回我的自定义 class MyClass的操作:

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

Is it possible to return response with 403 status code?是否可以使用 403 状态码返回响应? Or only way for it to return ActionResult ?还是它返回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. (这是.Net Core)由于签名,您将无法执行此操作。 You should change your signature to:您应该将您的签名更改为:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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