ASP.NET MVC在发送状态为500的响应时显示错误页面 [英] ASP.NET MVC showing error page on when sending responses with status 500

查看:359
本文介绍了ASP.NET MVC在发送状态为500的响应时显示错误页面的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在服务器中执行以下操作:

  [HttpPost] 
public JsonResult SearchContracts(SearchViewModel vm)
{
List< string>错误;

if(IsValid(vm,out errors))
{
返回Json(service.Search(vm),JsonRequestBehavior.AllowGet);
}
其他
{
HttpContext.Response.StatusCode = 500;

返回Json(new {Errors = errors},JsonRequestBehavior.AllowGet);
}
}

在本地,效果很好。当请求无效时,它返回带有错误的JSON,并且响应的HTTP状态代码的 500



部署后,IIS将返回给我:



解决了编辑 web.config 并添加以下属性:

 < system.webServer> 
...
< httpErrors existingResponse =PassThrough>< / httpErrors>
...
< /system.webServer>

另一个奇怪的微软故事。


I have the following action in the server:

[HttpPost]
public JsonResult SearchContracts(SearchViewModel vm)
{
    List<string> errors;

    if (IsValid(vm, out errors))
    {
        return Json(service.Search(vm), JsonRequestBehavior.AllowGet);
    }
    else
    {
        HttpContext.Response.StatusCode = 500;

        return Json(new { Errors = errors }, JsonRequestBehavior.AllowGet);
    }
}

Locally, it works great. When the request is not valid, it returns a JSON with the errors and the response has a 500 for the HTTP status code.

When deployed, instead of the JSON described, IIS is returning me this famed error page.

Here is the web.config with the detail for the customErrors section:

<customErrors mode="Off" defaultRedirect="~/error.htm">
    <error statusCode="404" redirect="~/errorhandling/pagenotfound" />
</customErrors>

I tried turning it to On but neither worked.

Where should I change to stop recieving that ugly error page instead of my beauty JSON?

Edit:

I changed the status code to 400, now I am getting the text Bad Request as a response, instead of the JSON:

解决方案

From this link:

Solved editing the web.config and adding the following attribute:

<system.webServer>
    ...
    <httpErrors existingResponse="PassThrough"></httpErrors>
    ...
</system.webServer>

Another bizarre Microsoft story.

这篇关于ASP.NET MVC在发送状态为500的响应时显示错误页面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆