开发者

Calling a WCF Rest method with JSON as the Response Format asks to download the file?

开发者 https://www.devze.com 2023-01-18 07:37 出处:网络
This is related the following que开发者_运维技巧stion, but I thought I would create another post so that question would not become too convoluted:

This is related the following que开发者_运维技巧stion, but I thought I would create another post so that question would not become too convoluted:

When I changed the ResponseFormat to XML, the browser return the response correctly, but if I try to use JSON as the ResponseFormat, it asks me if I want to download the file. Just to see what is in the file, I downloaded it and it contains the response in plain text json format. Is there some configuration I am missing?


This is exactly as expected. Here's why.

The content type of WCF JSON responses is "application/json". Most common browsers do NOT display content of this content type inline, since they do not know how to deal with "application/json" content. In other words, they don't treat it the same way as plain text or HTML or XML or JPEG content.

There's nothing wrong with the service, and there is no setting in WCF or IIS that will let you configure the service or service-side in such a way that your browser is no longer prompting you for a download.

You could try to change how IE or your browser handles content types, though. Here's a possible starting point: http://msdn.microsoft.com/en-us/library/ms775148%28v=vs.85%29.aspx


It probably has to do with the MIME type in the JSON http response not being set to something your browser "thinks" it should open right in the page.

What is your WebMessageBodyStyle set to in your WebGet attribute?

See http://msdn.microsoft.com/en-us/library/system.servicemodel.web.webgetattribute.aspx

0

精彩评论

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