How to configure WebMethods to use DataContractJsonSerializer
It appears that ASP.NET web methods use th开发者_如何学运维e JavaScriptSerializer, as it ignores my [DataContract] and [DataMember] attributes on my objects.
I have an object that looks like this:
[DataContract]
public class MyObject
{
[DataMember("firstField")]
public string FirstField { get; set; }
[DataMember("secondField")]
public string SecondField { get; set; }
}
My web method looks like this:
[WebMethod]
public static IEnumerable<MyObject> GetData() { ... }
Among other things, I want the JSON property names to come out as firstField rather than FirstField.
Is there a way to configure WebMethods to use DataContractJsonSerializer so that it respects the DataMember annotations?
[WebMethod]
and any of the Data Contract serializers are totally unrelated pieces of technology, and they cannot be mixed. If you want to use DataContractJsonSerializer, then you need to use WCF, not the old ASMX technology used by [WebMethod]
.
精彩评论