开发者

Cannot receive JSON exception message/content type on remote requests

I have a ASP.NET MVC3 application, which uses JSON to comunicate with a Flash UI.

I´m use an ActionFilterAttribute to handle JSON exceptions (from Handle JSON Exceptions Gracefully in ASP.NET MVC 2: http://www.dotnetcurry.com/ShowArticle.aspx?ID=496):


public class HandleJsonExceptionAttribute : ActionFilterAttribute
{
    public override void OnActionExecuted(ActionExecutedContext filterContext)
    {
        if (filterContext.Exception != null)
        {
            filterContext.HttpContext.Response.StatusCode = (int)System.Net.HttpStatusCode.InternalServerError;
            filterContext.Result = new JsonResult()
            {
                JsonRequestBehavior = JsonRequestBehavior.AllowGet,
                Data = new
                {
                    filterContext.Exception.Message,
                }
            };
            filterContext.ExceptionHandled = true;
        }
    }
}

It´s works ok when executes on localhost, details from fiddler:


HTTP/1.1 500 Internal Server Error
Cache-Control: private
Content-Type: application/json; charset=utf-8
Server: Microsoft-IIS/7.5
X-AspNetMvc-Version: 3.0
X-AspNet-Version: 4.0.30319
X-Powered-By: ASP.NET
Date: Mon, 11 Apr 2011 19:05:21 GMT
Content-Length: 34

{"Message":"No está autenticado"}

But, when executed from remote clients, on t开发者_运维问答he LAN for example, I get the response in "Content-Type: text/html" instead "Content-Type: application/json;" and the content is a standard html error page:

500 - Internal server error. There is a problem with the resource you are looking for, and it cannot be displayed.


HTTP/1.1 500 Internal Server Error
Cache-Control: private
Content-Type: text/html
Server: Microsoft-IIS/7.5
X-AspNetMvc-Version: 3.0
X-AspNet-Version: 4.0.30319
X-Powered-By: ASP.NET
Date: Mon, 11 Apr 2011 19:07:53 GMT
Content-Length: 1208

What or where I need to configure something to get the JSON response on remote requests?

I need the flash UI receive a http 500 error, but with the json message instead the html.


Looking at the article the javascript only seems to be wire up for local requests. What you need is to be using jsonp. (json with padding). This will allow you to do proper cross domain request returning a json object.

Further info can be found here and here.


I had a same problem, I solved with this code in web.config

<httpErrors existingResponse="PassTrough"></httpErrors> 
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜