2013-03-18 35 views
0

我已經成功地實現$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); 

回答

1

PageResult不能XmlSerializer序列化,因爲它不具有公共,無參數構造函數。但是沒有任何東西阻止你定義自己的具有公共無參數構造函數的相似類型。這應該很簡單。我建議看看PageResult的源代碼並採用類似的方法。