Making of JSON Webservice using C# .NET

前端 未结 5 646
隐瞒了意图╮
隐瞒了意图╮ 2021-01-03 12:27

I am trying to make JSON webservice in C# .NET. A json string is returning by web method but it contains xml structure like:

  

        
5条回答
  •  孤城傲影
    2021-01-03 12:43

    This is also not a problem when using ServiceStack, i.e. every result you return get's automatically converted in the Response ContentType you want, i.e. this is the full code of a simple web service that can be called via all HTTP VERBS (GET,POST,PUT,DELETE) on all the supported formats (no config required), i.e. JSON, XML, HTML, JSV, CSV, SOAP even by a direct HTML Form x-www-form-urlencoded or QueryString request:

    public class Hello {
        public string Name { get; set; }
    }
    
    public class HelloResponse {
        public string Result { get; set; }
    }
    
    public class HelloService : IService {
        public object Execute(Hello request)
        {
            return new HelloResponse { Result = "Hello, " + request.Name };
        }
    }
    

    You can override the response you get with the Accept:application/json HTTP Header or simply adding the ?format=json on the QueryString.

    See the ServiceStack's Hello World Example to see a live example the above web services.

提交回复
热议问题