ASP.net Web API: change class name when serializing ASP.net Web API: change class name when serializing asp.net asp.net

ASP.net Web API: change class name when serializing


I can't see why the class name should make it into the JSON-serialized data, but as regards XML you should be able to control the type name via DataContractAttribute, specifically via the Name property:

using System.Runtime.Serialization;[DataContract(Name = "Product")]public class ProductDTO{    [DataMember(Name="ProductId")]    public Guid Id { get; set; }    [DataMember]    public string Name { get; set; }    // Other properties}

DataContractAttribute is relevant because the default XML serializer with ASP.NET Web API is DataContractSerializer. DataContractSerializer is configured through DataContractAttribute applied to serialized classes and DataMemberAttribute applied to serialized class members.


An option is to use the default .Net Serialization attributes for this:

[DataContract(Name = "Product")]public class ProductDTO{    [DataMember(Name = "ProductId")]    public Guid Id { get; set; }    [DataMember]    public string Name { get; set; }    // Other properties}