返回一个纯XML(无子元素)与网页API
问题描述:
我怎么能返回仅由它的根元素组成的一个非常简单的XML?如: <?xml version="1.0" encoding="UTF-8"?> <myelement> somevalue </myelement>
返回一个纯XML(无子元素)与网页API
问题是没有子元素,所以我不知道什么类型的POCO类我应该写,以获得该输出。 我一直想是返回一个字符串,但我最终
<string>somevalue</string>
我是新来的的WebAPI所以任何建议将不胜感激!
答
我敢打赌,这不是最好的解决方案,但它现在正在为:
public class MyCustomFormatter : ActionFilterAttribute
{
public override void OnActionExecuted(HttpActionExecutedContext actionExecutedContext)
{
HttpResponseMessage response = actionExecutedContext.Response;
var contentType = response.Content.Headers.ContentType;
var oldContents = response.Content.ReadAsStringAsync().Result;
oldContents = oldContents.Replace("string", "myelement");
response.Content = new StringContent("<?xml version=\"1.0\" encoding=\"utf-8\"?>" + oldContents);
response.Content.Headers.ContentType = contentType;
actionExecutedContext.Response.Content = response.Content;
}
}
您要自定义从网络API发送XML? – 2013-02-22 15:13:17
嗨,是的,我正在寻找一种更简单的方式来返回像第一个片段的自定义XML。 – 2013-02-23 23:09:52