返回Mvc.JsonResult加上设置的Response.StatusCode [英] Return Mvc.JsonResult plus set Response.StatusCode

查看:490
本文介绍了返回Mvc.JsonResult加上设置的Response.StatusCode的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

项目:在.net 4.0框架下运行的ASP MVC 4:

Project: ASP MVC 4 running under .net 4.0 framework:

在VS 2010 Express下运行应用程序(或在本地计算机上的IIS 7.5下部署并运行)时,操作的以下结果(伪代码)按预期工作

When running an application under VS 2010 express (or deployed and running under IIS 7.5 on my local machine) the following (pseudocode) result from an action works as expected

[HttpPost]
public ActionResult PostWord(Model model)
{
   ....
   Response.StatusCode = 400;
   Return new JsonResult { data = new {fieldName = "Word", error = "Not really a word!" } };

(并且我已经分配了JsonResult对象的ContentType和ContentEncoding属性,但在行为上没有区别)

(and I have assigned ContentType and ContentEncoding properties of the JsonResult object, with no difference in behaviour)

将可部署对象移动到Web主机上(使用IIS 7)时,firebug告诉我响应是预期的(400),但是响应中没有JSON(即没有任何文本).如果我删除行

When the deployable is moved onto a web host (using IIS 7), firebug is telling me that the response is as expected (400) but there is no JSON in the response (ie there is no text of any kind). If I remove the line

Response.StatusCode = 400;

从该操作中,JSON完美地形成在响应中,但是响应状态代码当然是200(OK),这会干扰正在使用的javascript和适当的函数调用.

from the action, the JSON is perfectly formed in the response, but of course the response status code is 200 (OK), which interferes with the consuming javascript and appropriate function call.

关于可能发生的事情以及如何解决此问题的任何想法?谢谢

Any thoughts on what might be going on and how to fix this? Thank you

推荐答案

我遇到了完全相同的问题;为了确保正确的答案未包含在评论中(对我而言),我想重申@Sprockincat的评论:

I had this exact same problem; in order to make sure that the correct answer is not buried in the comments (as it was for me), I want to reiterate @Sprockincat's comment:

至少对我来说,这确实是IIS自定义错误的问题,可以通过以下方式解决:

For me at least, it was indeed an issue with IIS Custom errors, and can be solved with:

Response.TrySkipIisCustomErrors = true;

@Sprockincat-您应该为此功劳.我只是将其显示出来,因为它对难以诊断的问题进行了微妙的修复.

@Sprockincat - you should get credit for this. I'm just making it more visible because it's such a subtle fix to a problem that is quite difficult to diagnose.

这篇关于返回Mvc.JsonResult加上设置的Response.StatusCode的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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