reg*_*ahn 5 .net c# asp.net-mvc json
我有一个奇怪的问题,返回400状态代码与json错误.
在我的控制器中,我有类似的东西:
if(!ModelState.IsValid)
{
string[] errors = ModelState.Values
.SelectMany(x => x.Errors)
.Select(x => x.ErrorMessage).ToArray<string>();
Response.StatusCode = (int)System.Net.HttpStatusCode.BadRequest;
return Json(new { success = false, errors = errors }, JsonRequestBehavior.DenyGet);
}
Run Code Online (Sandbox Code Playgroud)
这在我的开发机器上工作正常.我可以在ajax错误方法中得到错误.但是当我部署到服务器时,服务器不再返回JSON.我总是得到responseText而不是reponseJSON.如果我删除Response.StatusCode它工作正常.
这让我相信当我设置Response对象的StatusCode属性时函数'返回'.这有发生在其他人身上吗?谁知道解决方案?
reg*_*ahn 11
我终于弄明白了问题所在.在这里发布这个作为其他任何可能正在为此而拔毛的人的答案.
设置以下内容:
Response.TrySkipIisCustomErrors = true;
Run Code Online (Sandbox Code Playgroud)
在设置状态代码之前,请确保已设置此项.我还想出了为什么它在我的本地机器上运行而不在test/uat服务器上运行.在我的web.config,CustomErrors设置为关闭,而在服务器上设置为开.
似乎服务器" 返回 ",只要它看到BadRequest写入的状态代码Response.