[英]XML namespace missing in web api response
我有一堂課叫Customer
[Serializable]
[DataContract]
[XmlRoot(Namespace = "http://noatariff.com")]
public class Customer
{
public Customer()
{
DateTime now = DateTime.Now;
string datePart = now.ToString("yyyy-MM-dd");
string timePart = now.ToString("HH:mm:ss.fffzzz");
this.TimeReceived = String.Format("{0}T{1}", datePart, timePart);
}
[DataMember]
[XmlElement(Namespace = "http://noatariff.com")]
public string TimeReceived { get; set; }
}
Web API代碼
[HttpGet]
[Route("ping")]
public HttpResponseMessage GetCustomerTime()
{
Customer cust = new Customer();
HttpResponseMessage resp = Request.CreateResponse<Customer>(HttpStatusCode.OK, cust, new XmlMediaTypeFormatter(), "application/xml");
return resp;
}
當我訪問我的方法時,得到如下響應:
<Customer xmlns:i="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://schemas.datacontract.org/2004/07/PMPI_InterconnectService.Controllers">
<TimeReceived>2016-09-07T15:35:50.658-05:00</TimeReceived>
<head/>
</Customer>
我想獲得名稱空間信息作為響應。
我缺少什么?
<mcn:Customer xmlns:mcn="http://noatariff.com">
<mcn:TimeReceived>2016-09-07T15:46:46.845-05:00</mcn:TimeReceived>
</mcn:Customer>
只是嘗試這個:
[DataContract(Namespace = "http://noatariff.com")]
public class Customer
{
public Customer()
{
DateTime now = DateTime.Now;
string datePart = now.ToString("yyyy-MM-dd");
string timePart = now.ToString("HH:mm:ss.fffzzz");
this.TimeReceived = String.Format("{0}T{1}", datePart, timePart);
}
[DataMember]
public string TimeReceived { get; set; }
}
如果您將DataContract
用作序列化器,則不需要其他任何東西。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.