【发布时间】:2015-10-16 10:46:11
【问题描述】:
我有一个 web api,其中全局配置被配置为使用: XmlMediaTypeFormatter
我的问题是我不会用一个新的控制器来扩展这个 web api,而是使用 JsonMediaTypeFormatter。
是否可以仅将一个 API Controller 类的 MediaTypeFormatter 更改为 JSON?
我的问题是没有返回 JSON,我已经通过返回 HttpResponseMessage 来解决这个问题:
return new HttpResponseMessage
{
Content = new ObjectContent<string>("Hello world", new JsonMediaTypeFormatter()),
StatusCode = HttpStatusCode.OK
};
这是我得到问题的请求。如果我有一个具有两个属性的对象:
public class VMRegistrant
{
public int MerchantId { get; set; }
public string Email { get; set; }
}
我的控制器操作将 VMRegistrant 作为参数:
public HttpResponseMessage CreateRegistrant(VMRegistrant registrant)
{
// Save registrant in db...
}
但问题是当我使用 JSON 调用操作时它失败了。
【问题讨论】:
-
是否在内容类型标头中包含“application/json”?
-
是的,我确定,刚刚又做了一次检查。用提琴手检查。
-
不确定这是否可行,但请尝试使用
[FromBody]属性。如果管道仅配置为 xml,则不确定它是否会起作用。
标签: c# asp.net-web-api