You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

WebAPI返回BadRequest时响应尾部多出BadRequest字样如何解决?

问题:移除API响应末尾的"BadRequest"字样

我有一个简单的API,代码如下:

[HttpGet]
[Route("problem")]
public ActionResult Prob()
{
    _logger.Debug($"HealthController.Accept");

    return Problem("WTF?", statusCode: (int)HttpStatusCode.BadRequest);
}

使用Postman调用该接口时,得到如下响应:

{
    "type": "https://tools.ietf.org/html/rfc7231#section-6.5.1",
    "title": "Bad Request",
    "status": 400,
    "detail": "WTF?",
    "traceId": "00-597a16e01489876a399d2e10e02789f7-20900f8e949848ac-00"
}BadRequest

而我期望的响应只有JSON部分:

{
    "type": "https://tools.ietf.org/html/rfc7231#section-6.5.1",
    "title": "Bad Request",
    "status": 400,
    "detail": "WTF?",
    "traceId": "00-597a16e01489876a399d2e10e02789f7-20900f8e949848ac-00"
}

请问如何移除响应末尾拼接的BadRequest字样?


更新:有评论指出可能是中间件问题。我在Startup.cs中发现了app.UseErrorHandling();:

app.UseCors("CorsPolicy");
app.UseDefaultFiles();
app.UseStaticFiles();
app.UseRouting();
// app.UseErrorHandling();
app.UseAuthentication();
app.UseAuthorization();
app.UseEndpoints(ep => { ep.MapControllers(); });

将该代码注释后,响应恢复正常。感谢@nikstra的建议,这个自定义授权错误处理中间件是我在共享库中开发的,之前完全忘记了它的存在。


内容的提问来源于stack exchange,提问作者ISZ

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.30 01:13:19