我有一个返回 PageResult 的 ODataController。
API 示例:
public PageResult<Customer> Get(ODataQueryOptions options) {
// cut some stuff out...
PageResult<Customer> result = new PageResult<Customer>(
searchResults as IEnumerable<Customer>,
Request.GetNextPageLink(),
Request.GetInlineCount());
return result;
当我调试它时,它似乎很好并且有一个正确构建的 PageResult 类来返回。在网络端..
网页示例
using (var client = new HttpClient()) {
client.BaseAddress = new Uri(testURL);
string searchUrl = "api/customer?$top=1&$skip=0";
client.DefaultRequestHeaders.Accept.Add(MediaTypeWithQualityHeaderValue.Parse("application/json;odata=verbose"));
HttpResponseMessage response = client.GetAsync(searchUrl).Result;
响应是 StatusCode 406,并带有说明内容 Not Acceptable 原因短语。如果我定义一个新的 MediaTypeWithQualityHeaderValue("application/json"),它也会执行此操作。
我需要更改什么才能在 Controller 中成功使用此 Api,然后再将其传递到 View ?
最佳答案
我认为您缺少构建 OData 服务的前两个步骤。 ODataController,顾名思义,仅适用于 OData 路由。您需要构建代表 OData 服务的 EDM 模型,并添加公开该 EDM 模型的 OData 路由。引用这个official documentation和博客 post了解如何构建 OData 服务的详细信息。
关于asp.net - 接收 OData.PageResult<T> 时如何避免 406?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15625794/