我希望我的 WEB API 方法将 XML 对象返回给调用应用程序。目前它只是将 XML 作为字符串对象返回。这是不可以吗?如果是这样,您如何告诉 webapi get 方法它正在返回 XML 类型的对象?
Thanks
编辑:Get 方法的示例:
[AcceptVerbs("GET")]
public HttpResponseMessage Get(int tenantID, string dataType, string ActionName)
{
List<string> SQLResult = MyWebSite_DataProvidor.DB.spReturnXMLData
("SELECT * FROM vwContactListing FOR XML AUTO, ELEMENTS").ToList();
string AllResults = "";
for (int i = 0; i < SQLResult.Count - 1; i++)
{
AllResults += SQLResult[i];
}
string sSyncData = "<?xml version=\"1.0\"?> " + AllResults;
HttpResponseMessage response = new HttpResponseMessage();
response.Content = new StringContent(sSyncData);
return response;
}
这有点老套,因为我仍处于原型设计阶段。当我能证明它可行时将重构。
如果您不希望控制器决定返回对象类型,则应该将方法返回类型设置为System.Net.Http.HttpResponseMessage
并使用以下代码返回 XML。
public HttpResponseMessage Authenticate()
{
//process the request
.........
string XML="<note><body>Message content</body></note>";
return new HttpResponseMessage()
{
Content = new StringContent(XML, Encoding.UTF8, "application/xml")
};
}
这是始终从 Web API 返回 XML 的最快方法。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)