ASP.NET 核心错误处理错误信息
ASP.NET core error handling error message
我开始着手 ASP.NET 核心错误处理,我注意到我收到格式错误
{
"errors": {},
"type": "https://tools.ietf.org/html/rfc7231#section-6.5.1",
"title": "One or more validation errors occurred.",
"status": 400,
"traceId": "|caeb2316-47fb93bd8a17bc0a."
}
我正在尝试获取更详细的错误消息,但我无法摆脱此消息。
我只是按照 Handle errors in ASP.NET Core 上的说明操作,但错误总是存在。
如何摆脱这种格式的错误消息?
使用以下代码,您可以创建自定义错误响应或自定义问题详细信息:
services.AddControllers()
.ConfigureApiBehaviorOptions(o =>
{
o.InvalidModelStateResponseFactory = context =>
{
var problemsDetailsFactory = context.HttpContext.RequestServices
.GetRequiredService<ProblemDetailsFactory>();
var problemDetails = problemsDetailsFactory.CreateValidationProblemDetails(
context.HttpContext,
context.ModelState);
problemDetails.Detail = "Custom Details";
problemDetails.Instance = context.HttpContext.Request.Path;
problemDetails.Type = "https://tools.etf............";
//problemDetails.Status = StatusCodes.Status422UnprocessableEntity;
problemDetails.Status = StatusCodes.Status400BadRequest;
problemDetails.Title = "One or more errors on input occured";
return new BadRequestObjectResult(problemDetails)
{
//ContentTypes= {"application/custom+json"}
};
};
});
我开始着手 ASP.NET 核心错误处理,我注意到我收到格式错误
{
"errors": {},
"type": "https://tools.ietf.org/html/rfc7231#section-6.5.1",
"title": "One or more validation errors occurred.",
"status": 400,
"traceId": "|caeb2316-47fb93bd8a17bc0a."
}
我正在尝试获取更详细的错误消息,但我无法摆脱此消息。
我只是按照 Handle errors in ASP.NET Core 上的说明操作,但错误总是存在。
如何摆脱这种格式的错误消息?
使用以下代码,您可以创建自定义错误响应或自定义问题详细信息:
services.AddControllers()
.ConfigureApiBehaviorOptions(o =>
{
o.InvalidModelStateResponseFactory = context =>
{
var problemsDetailsFactory = context.HttpContext.RequestServices
.GetRequiredService<ProblemDetailsFactory>();
var problemDetails = problemsDetailsFactory.CreateValidationProblemDetails(
context.HttpContext,
context.ModelState);
problemDetails.Detail = "Custom Details";
problemDetails.Instance = context.HttpContext.Request.Path;
problemDetails.Type = "https://tools.etf............";
//problemDetails.Status = StatusCodes.Status422UnprocessableEntity;
problemDetails.Status = StatusCodes.Status400BadRequest;
problemDetails.Title = "One or more errors on input occured";
return new BadRequestObjectResult(problemDetails)
{
//ContentTypes= {"application/custom+json"}
};
};
});