Xaisoft
Xaisoft

Reputation: 46641

When I call a REST method, it asks me if I want to Download the File

I have a simple WCF Rest Service with one method. The interface is defined as:

[ServiceContract]
public interface IHelloRest
{
    [OperationContract]
    [WebGet(UriTemplate = "json/hello/{name}", ResponseFormat = WebMessageFormat.Json)]
    string Hello(string name);
}

The implementation is defined as:

public string Hello(string name)
{
   return string.Format("Hello {0}.  You called my Hello method", name);
}

I deployed this service to IIS 7 running on Windows Server 2008 and here are the steps I did to add the service:

After doing the above steps, there are a few issues I run into:

When I browse to http://localhost/HelloRestService.svc/json/hello/xaisoft, it asks me if I want to download the file. If I download it and open it, it contains the response in json format. On my local machine, when I hosted this in IIS, it worked fine, but on this remote machine, it only asks me if I want to download the file.

The other issue is that I don't want the host to be localhost, I want something like demo.rest.com, so I would browse to http://demo.rest.com/json/hello/xaisoft, but if I change the host to demo.rest.com and try to browse the service now, it attempts to go to:

http://demo.rest.com/HelloRestService.svc, but says the Internet Explorer cannot display the webpage.

Upvotes: 0

Views: 658

Answers (1)

marc_s
marc_s

Reputation: 755013

That is the normal, default, expected behavior - IIS and IE don't know how to deal with a JSON result, so the best option is to offer to download the file so you can store and view it.

JSON is not designed and intended to be called directly in the browser - if you want to display stuff directly by browsing to them, use XML.

JSON is designed to be sent back to your client app (web app or whatever) as a small payload (e.g. "downloaded" as a chunk of data) and then interpreted by Javascript and turned into HTML markup (typically).

So I don't think there's anything wrong here - if something is odd, then it's the fact that on your local system, it works "just fine" (what does that even really mean?? What happens??)

Upvotes: 4

Related Questions