Problem with Internet Explorer and JSONResult in MVC

May 05, 2014 by Anuraj

.Net ASP.Net ASP.Net MVC

While working with the ASP.Net MVC project, one of my QA team member reported a strange issue, while uploading a file, Internet Explorer 9, showing a download file dialog like this.

File Download dialog while posting data

And the controller action is similar like this.

[HttpPost, Authorize]
public JsonResult Upload(FormCollection collection)
    return Json("Uploaded successfully");

This code is working fine in other browsers like Chrome, Firefox, and later versions of Internet Explorer(I tried with IE 11).

Later I found the solution, it was due to content type handling problem with Internet Explorer. You can resolve it by using the other overloads available for JsonResult class, in which you can specify the content type. And here is the modified version of the action method.

public JsonResult Upload(FormCollection collection)
    return Request.AcceptTypes.Contains("application/json") ? 
        Json("Uploaded successfully") : Json("Uploaded successfully", "text/plain");

Happy Programming :)

Copyright © 2024 Anuraj. Blog content licensed under the Creative Commons CC BY 2.5 | Unless otherwise stated or granted, code samples licensed under the MIT license. This is a personal blog. The opinions expressed here represent my own and not those of my employer. Powered by Jekyll. Hosted with ❤ by GitHub