WebApi强制返回XML的动作 [英] WebApi force action to return xml

查看:136
本文介绍了WebApi强制返回XML的动作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这个动作:

public IHttpActionResult SearchFor(int aboItemType, DTO.FilterColumns filter)
{
    //Do stuff...
    return Ok<DataSet>(ds);
}

我的客户这样做:

client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/xml"));

var response = client.PostAsJsonAsync(myurl).Result;
if (response.IsSuccessStatusCode)
{
    var results = HttpUtility.HtmlDecode(response.Content.ReadAsStringAsync().Result);
}

以上情况非常适用.但是,如果我注释了接受"行,则该操作将以json格式返回数据集.

The above scenario works perfectly. However, if I comment the Accept line, the action returns the dataset in json format.

我想强制执行这一特定操作,以始终将结果发送到xml中.这可能吗?也许带有属性?

I would like to force this one particular action to always send the result in xml. Is this possible? Maybe with an attribute?

推荐答案

我使用了 onfiguration.Formatters.XmlFormatter

public IHttpActionResult Get()
{
 ...
  return Content(HttpStatusCode.OK, Model, Configuration.Formatters.XmlFormatter);
}

这篇关于WebApi强制返回XML的动作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆