Mic*_*uso 5 c# http-status-code-404 asp.net-web-api asp.net-core
控制器现在具有CreatedAtRoute()201、400 HttpBadRequest()等的功能。我看不到500的功能HttpInternalServerError()。
但是,有HttpStatusCodeResult一个我可以创建并返回的类:
[HttpPost]
public IActionResult Post([FromBody]string something)
{
...
try{
}
catch(Exception e)
{
return new HttpStatusCodeResult((int)HttpStatusCode.InternalServerError);
}
}
Run Code Online (Sandbox Code Playgroud)
但我想从中返回一些信息e。这对于现场直播来说可能是不好的做法,但是在进行测试时,我们希望从API调用返回的主体中查看错误。
HttpStatusCodeResult没有object用于提供元数据的type 或任何属性。
该怎么办?我不想返回400,因为这不是内部服务器错误的意思。
return ResponseMessage(Request.CreateErrorResponse(HttpStatusCode.InternalServerError, "Message describing the error here"));
Run Code Online (Sandbox Code Playgroud)
或者
return InternalServerError(new Exception("SOME CUSTOM MESSAGE"));
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
4427 次 |
| 最近记录: |