gpt4 book ai didi

asp.net-web-api - 如何让 ASP.NET Web API 只返回 XML?

转载 作者:行者123 更新时间:2023-12-04 17:36:38 25 4
gpt4 key购买 nike

我正在尝试向 ASP.NET Web API 发送一个 get 请求并返回一个 XML 以在我的 Android 应用程序中解析它。当我通过网络浏览器尝试链接时它返回 XML,但当 Android 应用程序发送请求时它返回 JSON。如何以仅发送 XML 的方式修复它?谢谢

最佳答案

如果您不打算提供 JSON,则可以删除 JSON 格式化程序:

var formatters = GlobalConfiguration.Configuration.Formatters;
formatters.Remove(formatters.JsonFormatter);

您还可以明确指定要在您的操作中使用的格式化程序:
public object Get()
{
var model = new
{
Foo = "bar"
};

return Request.CreateResponse(HttpStatusCode.OK, model, Configuration.Formatters.XmlFormatter);
}

关于asp.net-web-api - 如何让 ASP.NET Web API 只返回 XML?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16713549/

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