在内容协商的差异MVC 4集合和单一值之间? [英] Differences in content negotiation between collections and single values in MVC 4?

查看:142
本文介绍了在内容协商的差异MVC 4集合和单一值之间?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我利用新的MVC4 ApiController打造出了一个搜索界面;是这样的:

I'm leveraging the new MVC4 ApiController to build out a search interface; something like this:

public IEnumerable<RecordSummaryType> Get( ... )
{
    var list = MyService.GiveMeTheList( ... );
    return list;
}

public SingleRecordDetailType Get(long id)
{
    var result = MyService.GiveMeASingleValue(id);
    return result;
}

由于某些原因,在这种情况下,IEnumerable的通话荣誉内容协商 - 例如,当我通过应用程序/ XML请求接受头部返回XML,当我通过应用程序/ JSON返回JSON - 但SingleRecordType调用仅返回JSON,即使你问XML。

所以我的问题是 - 在MVC 4处理集合了单值类型的方式有差异或者,更可能的是,在MVC有挂钩,人们可以在不经意间禁止内容协商?对于某些电话?

So my question is - are there differences in the way MVC 4 handles collections over single value types? Or, more likely, are there hooks in MVC where one can inadvertently disable content negotiation for certain calls?

推荐答案

这取决于是否SingleRecordDetailType可以通过XmlSerializer的序列化。 XmlSerializer的是ASP.NET的Web API默认XML格式,阅读的这里更多地了解它的局限性。

It depends whether the SingleRecordDetailType can be serialized by the XmlSerializer. The XmlSerializer is default XML formatter in ASP.NET Web API, read here to know more about its limitations.

如果网页API无法序列与请求的格式,将使用第一个格式化中能够序列化响应的列表响应(大部分时间,这是JSON格式)。

If Web API can't serialize the response with the requested formatter it will use the first formatter in the list which is capable of serializing the response (most of time this is JSON formatter).

你可以修改夜类通过XmlSerializer的可序列化或重新配置XML格式的UseDataContractSerialzier属性设置为true,使用数据契约序列化的解决方案。

As a solution you can modify yoru class to be serializable by XmlSerializer or reconfigure the XML formatter to use data contract serializer by setting the UseDataContractSerialzier property to true.

这篇关于在内容协商的差异MVC 4集合和单一值之间?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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