【问题标题】:OData paging with XmlSerializer使用 XmlSerializer 进行 OData 分页
【发布时间】:2013-03-18 16:33:29
【问题描述】:

我已经设法使用 ODataQueryOptions<T> 和 PageResult<T> 类使用 WebApi.OData (v 4.0.0) 实现 $inlinecount,如下所示:

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);

【问题讨论】:

    标签: c# asp.net-web-api odata


    【解决方案1】:

    PageResult 不能被XmlSerializer 序列化,因为它没有公共的、无参数的构造函数。但是没有什么能阻止你定义你自己的类似类型,它有一个公共的、无参数的构造函数。做起来应该很简单。我建议您查看PageResult 的源代码并采用类似的方法。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-05-29
      • 2020-09-14
      • 2019-09-07
      • 2023-04-03
      • 1970-01-01
      • 1970-01-01
      • 2014-09-01
      • 2017-08-16
      相关资源
      最近更新 更多