我的dotnet核心应用程序中有一个actionfilter,并希望在ajax调用返回错误结果时为用户生成正确的消息。 在客户端errorThrown参数显示给用户,我不想更改客户端脚本。 当ajax调用有异常(500状态代码)时,用户只得到“内部服务器错误”消息,我将显示更详细的信息。
我尝试使用return StatusCode(500, Json("Custom error"))
,return StatusCode(500, "Custom error")
但没有向用户显示的结果。
error: function (jqXHR, textStatus, errorThrown) {
$('#failed-info').text(errorThrown);
}
对我来说最好的解决方案是我可以提供自定义statusCode和statusText结果,例如statusCode = 700,StatusText =“出于安全原因,不允许这样做。”
发布于 2019-06-12 17:44:58
WEB API服务器端自定义消息示例
public class MyAPIController : ApiController
{
[HttpPost]
[Route("~/api/mypath/")]
public IHttpActionResult returnError([FromBody]StringModel value)
{
return Content(HttpStatusCode.Forbidden,"bla bla bla");
}
}
在示例中,它返回状态,Forbidden (403)
但您可以更改为您想要的。
但浏览器取决于状态可能会覆盖错误。
https://stackoverflow.com/questions/-100009094
复制相似问题