1

I am developing Asp.Net Core 3.1 API, Everything working as expected when I send a GET request from google chrome, Edge, Postman. But when I send GET request from internet explorer it starts to download a file default.json with the content as the response of GET request.

Defualt Action method:

public IEnumerable<string> Get()
{
    return new string[] { "Welcome" };
}

default.json content:

[
    "Welcome"
]

I search on the internet but could not find anything useful.

FVI, I have the same observation when I run the API using visual studio or the deployed API on the server using IIS.

IE Version: 11.900.18362.0

So I have to questions.

  1. Does IE not support this, Is this default behavior of IE?
  2. If Yes then how can it be fixed?
Asons
  • 84,923
  • 12
  • 110
  • 165
Vivek Nuna
  • 25,472
  • 25
  • 109
  • 197
  • 2
    I'm in favor of Ason's answer. The behavior is not specific to Asp.Net Core, it's a default behavior of IE. Besides, you could try to return the json as `text/plain` since IE does not know what to do with `application/json` contents. For more information, you could refer to [this thread](https://stackoverflow.com/questions/13943439/json-response-download-in-ie710) and [this thread](https://stackoverflow.com/questions/6114360/ie-prompts-to-open-or-save-json-result-from-server). – Yu Zhou Jul 13 '20 at 05:49
  • If my answer doesn't solve your question, what is missing?, and if, please accept it, so future users can see it did solve the issue described. – Asons Jul 26 '20 at 09:50
  • @Ason sorry for the delay :), accept + upvote – Vivek Nuna Jul 26 '20 at 09:53
  • No problem with that...just wanted to make sure I wasn't missing something :) – Asons Jul 26 '20 at 09:55

1 Answers1

3

This is IE default behavior, and comes down to it simply doesn't know how to treat content with mime types like */json, hence suggest a download.

Assuming this is for users in general, and you simply want to display the json data in a browser, you could convert the content server side to text.

public ContentResult Get()
{
    var jsondata = new string[] { "Welcome" };
    return Content(JsonSerializer.Serialize(jsondata));
}

If you are going to do something with the actual json data, which one usually does when consuming an api, you will use some kind of client side script (e.g. Ajax as in below sample, or similar) to get the content, and in those cases there won't be any problem, like the one you encountered.

var xhr = new XMLHttpRequest();
xhr.open('GET', '/api/your-method', true);
xhr.onload = function (e) {
    if (this.status == 200) {
        var jsonstring = this.responseText;
        // do something with the json string, e.g. JSON.parse(jsonstring)
    }
};
xhr.send();

Here's a couple of posts that suggests to change the registry, though they won't be viable unless it is for a local computer of your own (and if it is, picking a browser that works out-of-the-box must be easier).


Edit

As suggested in a comment, yet another option would be to change the mime type explicit:

Asons
  • 84,923
  • 12
  • 110
  • 165