的OData寻呼与XmlSerializer的
问题描述:
我已经成功地实现$inlinecount
与WebApi.OData(V 4.0.0)使用ODataQueryOptions<T>
和PageResult<T>
类是这样的:的OData寻呼与XmlSerializer的
POCO
public class Poco
{
public int id { get; set; }
public string name { get; set; }
public string type { get; set; }
}
控制器
[ActionName("Default")]
public PageResult<Poco> Get(ODataQueryOptions<Poco> queryOptions)
{
var data = new Poco[] {
new Poco() { id = 1, name = "one", type = "a" },
new Poco() { id = 2, name = "two", type = "b" },
new Poco() { id = 3, name = "three", type = "c" },
new Poco() { id = 4, name = "four", type = "d" },
new Poco() { id = 5, name = "five", type = "e" },
new Poco() { id = 6, name = "six", type = "f" },
new Poco() { id = 7, name = "seven", type = "g" },
new Poco() { id = 8, name = "eight", type = "h" },
new Poco() { id = 9, name = "nine", type = "i" }
};
var t = new ODataValidationSettings() { MaxTop = 2 };
queryOptions.Validate(t);
var s = new ODataQuerySettings() { PageSize = 1 };
IQueryable results = queryOptions.ApplyTo(data.AsQueryable(), s);
var next = Request.GetNextPageLink();
var count = Request.GetInlineCount();
return new System.Web.Http.OData.PageResult<Poco>(
results as IEnumerable<Poco>, next, count);
}
我从JSON切换到旧学校XmlSerializer时出现错误406。有谁知道这是否应该起作用?
var xml = GlobalConfiguration.Configuration.Formatters.XmlFormatter;
xml.UseXmlSerializer = true;
GlobalConfiguration.Configuration.Formatters.Remove(
GlobalConfiguration.Configuration.Formatters.JsonFormatter);
答
PageResult
不能XmlSerializer
序列化,因为它不具有公共,无参数构造函数。但是没有任何东西阻止你定义自己的具有公共无参数构造函数的相似类型。这应该很简单。我建议看看PageResult
的源代码并采用类似的方法。