【发布时间】:2014-10-08 11:42:57
【问题描述】:
在我的 Web API 应用程序中,如果我在 Get 方法中使用 OData 来过滤结果,我会得到不一致的结果。没有OData 过滤器,结果很好。 在使用 OData 时,序列化的处理方式是否不同? 我这样做是否正确?
使用 OData:
我不明白 $ref 条目是什么,为什么它们是随机的,为什么我不使用 OData 时得不到它们??
这是Web API 方法:
public HttpResponseMessage Get(System.Web.Http.OData.Query.ODataQueryOptions<Employee> options)
{
HttpResponseMessage response;
var employees = options.ApplyTo(_unitOfWork.EmployeeRepository.Get());
if (employees == null)
{
response = new HttpResponseMessage(HttpStatusCode.NotFound);
}
else
{
response = Request.CreateResponse(HttpStatusCode.OK, employees);
response.Content.Headers.Expires = new DateTimeOffset(DateTime.Now.AddSeconds(300));
}
return response;
}
这是要序列化的类:
public class Employee
{
[Key]
public string WinId { get; set; }
public string XML { get; set; }
public int EffectiveYear { get; set; }
public int FileKeeperGroupId { get; set; }
}
编辑 8/15
这是我的 WebApiConfig
var json = config.Formatters.JsonFormatter;
config.Formatters.Clear();
config.Formatters.Add(json);
config.Formatters.JsonFormatter.SerializerSettings.PreserveReferencesHandling =
Newtonsoft.Json.PreserveReferencesHandling.None;
【问题讨论】:
-
不清楚您使用 OData 的意义?您的意思是使用 ODataController 而不是 ApiController?能否也展示一下您使用 OData 的代码?
标签: c# json entity-framework asp.net-web-api odata