[英]ASP.NET Core How to output custom XML Model
I have a Web API built in C# core and I need to customize the XML output that one of my endpoints gives. 我有一个内置于C#核心中的Web API,我需要自定义一个端点提供的XML输出。
To configure my app to return XML and JSON, I use Accept Header like so: 要配置我的应用程序以返回XML和JSON,我可以这样使用Accept Header:
// Add framework services.
services
.AddMvc(options => {
options.RespectBrowserAcceptHeader = true;
})
//support application/xml
.AddXmlDataContractSerializerFormatters()
//support application/json
.AddJsonOptions(options => {
options.SerializerSettings.ContractResolver = new DefaultContractResolver();
});
And I have a model I return on a controller: 我有一个在控制器上返回的模型:
public class SageInvoice {
public long Id { get; set; }
public DateTime Created { get; set; }
public long? CustomerId { get; set; }
public long? JobId { get; set; }
public DateTime Date { get; set; }
public decimal Subtotal { get; set; }
public decimal VAT { get; set; }
public decimal Total { get; set; }
}
The XML comes out like so: XML如下所示:
<ArrayOfSageInvoice xmlns:i="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://schemas.datacontract.org/2004/07/AutomotiveDTO.DTOs.SageIntegration">
<SageInvoice>
<Created>2017-03-15T11:09:34.21</Created>
<CustomerId>1</CustomerId>
<Date>2017-03-15T00:00:00</Date>
<Id>2</Id>
<JobId>1</JobId>
<Subtotal>100.00</Subtotal>
<Total>120.00</Total>
<VAT>20.00</VAT>
</SageInvoice>
<SageInvoice>
<Created>2017-03-15T11:11:26.853</Created>
<CustomerId>2</CustomerId>
<Date>2017-03-15T00:00:00</Date>
<Id>3</Id>
<JobId i:nil="true"/>
<Subtotal>23532.00</Subtotal>
<Total>28238.40</Total>
<VAT>4706.40</VAT>
</SageInvoice>
</ArrayOfSageInvoice>
The consumer of my endpoint doesn't like it structured like this and wants me to make it look more: 我的端点的使用者不喜欢这样的结构,希望我让它看起来更多:
<?xmlversion="1.0"encoding="utf-8"?>
<Order>
<OrderSummary>
<OrderID>1</OrderID>
<OrderReference>ORDER1</OrderReference>
<OrderDate>2017-03-15</OrderDate>
<AccountNumber>ACCOUNT1</AccountNumber>
<CompanyName>Company 1</CompanyName>
<ContactTitle>Mr</ContactTitle>
<ContactFirst>Dave</ContactFirst>
<ContactLast>Dave</ContactLast>
<Address1/>
<Address2/>
<Town/>
<County/>
<Postcode/>
<Country/>
<Telephone>01234567890</Telephone>
<NumberOfItems>2</NumberOfItems>
<OrderValue>200</OrderValue>
<Downloaded>false</Downloaded>
</OrderSummary>
<OrderSummary>
...
</OrderSummary>
</Order>
How can I change my XML output to match the above without wrecking the Accept headers JSON + XML input/output functionality? 如何在不破坏Accept标头JSON + XML输入/输出功能的情况下更改XML输出以匹配以上内容?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.