0

所以我正在开发一个 Roku 频道将与之交互以发送和接收数据的 Web API。Roku SDK 有一个易于使用的内置 XML Parser,但唯一的问题是 Roku 只会解析包装在<rsp stat="ok"></rsp>元素中的 XML。我看不到如何或在何处覆盖 Web API 上的 XML 输出以将其与<rsp>元素一起包装。

所以我的问题是,如何覆盖 XML 格式化程序并<rsp stat="ok">在输出之前和</rsp>之后插入?

4

1 回答 1

1

如果您通过像这样删除 JSON 格式化程序来确保只返回 XML

config.Formatters.Remove(config.Formatters.JsonFormatter);

您可以使用消息处理程序为所有这样的响应盲目添加信封。

public class MyHandler : DelegatingHandler
{
    protected override async Task<HttpResponseMessage> SendAsync(
                                       HttpRequestMessage request,
                                            CancellationToken cancellationToken)
    {
        var response = await base.SendAsync(request, cancellationToken);
        string responseBody = "<rsp stat=\"ok\">" + 
                                  await response.Content.ReadAsStringAsync() +
                              "</rsp>";
        response.Content = new StringContent(
                          responseBody, Encoding.UTF8, "application/xml");
        return response;
    }
}
于 2013-07-19T16:21:17.377 回答