gpt4 book ai didi

asp.net - WebAPI 返回 XML

转载 作者:行者123 更新时间:2023-12-03 06:55:11 27 4
gpt4 key购买 nike

我希望我的 WEB API 方法将 XML 对象返回给调用应用程序。目前它只是将 XML 作为字符串对象返回。这是不可以吗?如果是这样,您如何告诉 webapi get 方法它正在返回 XML 类型的对象?

谢谢

编辑: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;
}

这有点hacky,因为我仍处于原型(prototype)设计阶段。当我能证明它可行时将重构。

最佳答案

如果您不希望 Controller 决定返回对象类型,则应将方法返回类型设置为 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 的最快方法。

关于asp.net - WebAPI 返回 XML,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11662028/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com