WebApi XML,Json格式自定義,IEnumerable<T>,ArrayOf

jackchain發表於2014-03-27

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;
        }

 

相關文章