views:

65

answers:

1

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:

  • I started IIS Manager
  • I Right-Clicked on Sites and Chose Add Web Site...
  • I put 'Test' for Site Name
  • For the application pool, I chose ASP.NE v4.0
  • For the physical path, I put the root folder of my service.
  • For Binding, I put http, I left IP address unassigned and I left the port at 80.
  • I did not give a Host name
  • I left Start Web Site immediately checked.
  • I clicked Ok and then browsed to my site.

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.

+3  A: 

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??)

marc_s
What I meant by "just fine" is that on IIS installed on local machine, when I do a http call that returns the response in JSON, it actually displays the JSON in the broswer, but if I remote into another machine (Windows Server 2008) and host the server on IIS there and browse to the same http url, that is where it asks me to download the file.
Xaisoft
@Xaisoft: OK, but that is "abnormal" - the normal behavior is to see a prompt to download the resulting .JSON file.
marc_s