WebApi XML,Json格式自定义,IEnumerable<T>,ArrayOf
global.ascx中application-start()
GlobalConfiguration.Configuration.Formatters.XmlFormatter.UseXmlSerializer = true;
[XmlIgnore] //生成XML结果的时候,忽略该字段 [JsonIgnore] //生成JSON结果的时候,忽略该字段 public Guid Id { get; set; }
当程序需要返回List<T>或者IEnumerable<T>多记录的时候,为了避免出现ArrayOf为XMLROOT的情况,需要自定义一个类,如下
namespace ElegantWM.DTO { [XmlRoot("EcResult")] //[XmlArrayItem("SmsReceived")] //[CollectionDataContract(Name = "EcResult", ItemName = "SmsReceived", Namespace = "")] public class EcSmsReceivedResult : List<EC_SmsReceived> { } }
[HttpPost] public EcSmsReceivedResult SmsReply() { EC_Customer customer = CheckCustomer(); var list = WMFactory.SmsReceived.GetSmsReceived(customer.Id); EcSmsReceivedResult rst=new EcSmsReceivedResult(); rst.AddRange(list); return rst; }