postman 提示界面如图:上面是自带提示,下面是我自定义。
{
"type": "https://tools.ietf.org/html/rfc7231#section-6.5.1",
"title": "One or more validation errors occurred.",
"status": 400,
"traceId": "|e83645f6-45a222f9ff0ab1de.",
"errors": {
"$.UserDesc": [
"Expected depth to be zero at the end of the JSON payload. There is an open JSON object or array that should be closed. Path: $.UserDesc | LineNumber: 6 | BytePositionInLine: 0."
]
}
}
{
"code": 400,
"msg": "执行方法中抛出异常错误",
"data": "url:/AteUser"
}
程序输出方式:await context.Response.WriteAsync(json);
执行方式:
public async Task Invoke(HttpContext context)
{
try
{
await _next(context);
}
catch (Exception ex)
{
if (ex is ServiceException serviceException)
{
await HandleExceptionAsync(context, serviceException.LogSn, ex.Message);
}
}
finally
{
if (context.Response.StatusCode >= 400&& context.Response.StatusCode < 599)
{
await HandleExceptionAsync(context, context.Response.StatusCode, "");
}
}
}
catch可正常抛出异常,finaly则会抛出两个对象异常
输出方式:
try
{
context.Response.Clear();
}
catch (Exception ex)
{
//throw ex;
}
return context.Response.WriteAsync(result);
context.Response.Clear()
之后 在write
是在clear之后进行写入的 问题补充那截图了
@suqq小白:
完整的代码发出来...
@czd890: 已发出来了