2

I created a new wcf rest service using .net 4. I have created a new service based on the example one and referenced it in the global.asax file so I can browse to it ok.

My problem is I get the results back in xml not json. This is my service code

  [ServiceContract]
[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
[ServiceBehavior(InstanceContextMode = InstanceContextMode.PerCall)]
// NOTE: If the service is renamed, remember to update the global.asax.cs file
public class CheckIfValid
{
    [OperationContract]
    [WebGet(UriTemplate = "/{id}/details.json",
            ResponseFormat = WebMessageFormat.Json)]
    public SampleItem Get(string id)
    {
        // TODO: Return the instance of SampleItem with the given id
        //throw new NotImplementedException();
        if(id=="123")
        return new SampleItem { Id = 1, StringValue = "Got it" };
        else
            return new SampleItem { Id = 2, StringValue = "Not valid fool..." };
    }
}

Very basic but I am just trying to get my head around it. The returned result looks like this

<SampleItem xmlns="http://schemas.datacontract.org/2004/07/WcfRestService1" xmlns:i="http://www.w3.org/2001/XMLSchema-instance"><Id>1</Id><StringValue>Got it</StringValue></SampleItem>

I have tried browsing to the help section in the service but the json example doesnt do anything.

Can anyone suggest what I should be doing differently?

Diver Dan
  • 9,953
  • 22
  • 95
  • 166

1 Answers1

0

Have you tried making the request via Fiddler instead of using a web browser? Many browsers will not render any JSON they get back.

Darrel Miller
  • 139,164
  • 32
  • 194
  • 243
  • regarding displaying json in the browser, see http://stackoverflow.com/questions/2483771/how-can-i-convince-ie-to-simply-display-application-json-rather-than-offer-to-dow – Cheeso Dec 17 '10 at 00:39
  • @Cheeso Nice to know that you can do that, but you still won't pry fiddler from my cold, dead hands. :-) Debugging REST services via a browser is just a massive exercise in frustration. – Darrel Miller Dec 17 '10 at 00:43
  • Thanks Darrel. Doh feel abit stupid for not testing in fiddler – Diver Dan Dec 17 '10 at 00:43
  • @Darrel - I use fiddler pretty much every day, so I agree with you. It's indispensable. There are so many cool features beyond just tracing the HTTP messages. You can intercept traffic, script modifications, set breakpoints. It's a great tool. Even so, sometimes it's nice to see json in the browser. – Cheeso Dec 17 '10 at 00:46
  • Is there any reason why this piece of jqeury is returning anything? – Diver Dan Dec 17 '10 at 02:33
  • @user293545 Looks good to me, but then I'm definitely not a jQuery guy. – Darrel Miller Dec 17 '10 at 02:56