Making of JSON Webservice using C# .NET

前端 未结 5 666
隐瞒了意图╮
隐瞒了意图╮ 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:58

    If you decorate your interface with attributes for request and response format you can get standard WCF to return and interpret proper json.

        [WebGet(UriTemplate = "user/{userid}", RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json)]
    

    The problem is, however, that WCF's innate DataContractJsonSerializer does not always return proper json. Its serialization of dictionaries is problematic at best, since it is serialized as a list of key/value-pairs. To remedy this one has to return Stream from the service methods and do the serialization by hand (using Json.NET or ServiceStack to perform the serialization). In such cases it is probably advisable to use WebAPI, but for some cases regular WCF can be used using the mentioned decorations.

提交回复
热议问题