none
. Net framework WCF service method to return the JSON? RRS feed

Answers

  • Hi,

    Please try to check the following example about return JSON data:

    In the IService1:

    namespace WcfJsonRestService
    {
        [ServiceContract]
    public interface IService1 { [OperationContract]
    [WebInvoke(Method = "GET",
                        ResponseFormat = WebMessageFormat.Json,
                        UriTemplate = "data/{id}")] Person GetData(string id); } }

    In the Service1.svc:

    public class Service1 : IService1
        {
           
            public Person GetData(string id)
            {
                        return new Person()
                           {
                               Id = Convert.ToInt32(id), 
                               Name = "Leo Messi"
                           };
            }
        }
    
        public class Person
        {
            public int Id { get; set; }
            public string Name { get; set; }
        }

    The result:

    To test the new service we will pass in the id value of 10 simply by opening our browser and pasting in the following URL: 

    http://localhost:8732/Service1/data/10

    json-browser


    Besides, the following article may also help you:
    #WCF rest service to get post JSON data and retrieve JSON data with DataContract:
    http://dotnetmentors.com/wcf/wcf-rest-service-to-get-or-post-json-data-and-retrieve-json-data-with-datacontract.aspx .

    Best Regards,
    Amy Peng


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.



    Monday, July 21, 2014 2:11 AM
    Moderator

All replies

    • Marked as answer by bestdragon Monday, July 21, 2014 2:12 AM
    • Unmarked as answer by bestdragon Monday, July 21, 2014 2:18 AM
    Friday, July 18, 2014 10:25 AM
  • Hi,

    Please try to check the following example about return JSON data:

    In the IService1:

    namespace WcfJsonRestService
    {
        [ServiceContract]
    public interface IService1 { [OperationContract]
    [WebInvoke(Method = "GET",
                        ResponseFormat = WebMessageFormat.Json,
                        UriTemplate = "data/{id}")] Person GetData(string id); } }

    In the Service1.svc:

    public class Service1 : IService1
        {
           
            public Person GetData(string id)
            {
                        return new Person()
                           {
                               Id = Convert.ToInt32(id), 
                               Name = "Leo Messi"
                           };
            }
        }
    
        public class Person
        {
            public int Id { get; set; }
            public string Name { get; set; }
        }

    The result:

    To test the new service we will pass in the id value of 10 simply by opening our browser and pasting in the following URL: 

    http://localhost:8732/Service1/data/10

    json-browser


    Besides, the following article may also help you:
    #WCF rest service to get post JSON data and retrieve JSON data with DataContract:
    http://dotnetmentors.com/wcf/wcf-rest-service-to-get-or-post-json-data-and-retrieve-json-data-with-datacontract.aspx .

    Best Regards,
    Amy Peng


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.



    Monday, July 21, 2014 2:11 AM
    Moderator