Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / web / ASP.NET

Internet Explorer 9 JsonResult Does Not Return Json in MVC3

5.00/5 (4 votes)
22 Nov 2015CPOL 12.3K  
Internet Explorer 8/9 jsonresult method treats response as downloadable json result

Introduction

In Internet Explorer 8 and Internet Explorer 9, the JsonResult ActionResult was returning the content-type of the JSON result as application/json, which seems to make Internet Explorer want to download it. but other browsers were working fine. So we need to override the Jsonresult method in order for it to work in all browsers.

The Code

So here, we use the base controller class that inherits from the controller class, and we set the content type. So you don't need to set content type individually.

JavaScript
public class BaseController : Controller
{
    protected new JsonResult Json(object data)
    {
        if (!Request.AcceptTypes.Contains("application/json"))
            return base.Json(data, "text/plain");
        else
            return base.Json(data);
    }
}

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)