【问题标题】:Can't Get $expand Working for a Single OData Entity With AsyncEntitySetController无法让 $expand 使用 AsyncEntitySetController 为单个 OData 实体工作
【发布时间】:2014-06-06 22:26:13
【问题描述】:

我无法在单个实体上使用 $expand,但它在集合上运行良好。我有以下异步 OData Web API 控制器:

public class AuthorController : AsyncEntitySetController<Author, int>
{
    private readonly IAuthorRepository _AuthorRepo = new AuthorRepository();

    // GET odata/<controller>
    [Queryable]
    public override async Task<IEnumerable<Author>> Get()
    {
        var authors = await _AuthorRepo.GetAll();
        return authors.AsQueryable();
    }

    // GET odata/<controller>/5
    [Queryable]
    public async Task<Author> Get([FromODataUri]int key)
    {
        var author = await _AuthorRepo.GetById(key);
        return author;
    }
}

通过https://api.acme.org/odata/Authors 请求收藏有效,通过https://api.acme.org/odata/Authors?$expand=Books 扩展作者的书籍也是如此。

但是,尝试使用上述通过 https://api.acme.org/odata/Authors(1)?$expand=Books 接受关键参数的 Get 方法扩展单个实体会产生以下错误:

找到多个与请求匹配的操作:获取类型 AcmeApi.Controllers.AuthorsController 获取类型 AcmeApi.Controllers.AuthorssController

在阅读this question 的答案后,我尝试通过将第二个 Get 方法更新为返回 SingleResult:

[Queryable]
public SingleResult<Account> Get([FromODataUri]int key)
{
    return SingleResult.Create<Account>(m_AccountRepo.GetById(key));
}

但这会导致以下编译错误:

参数 1:无法从 'System.Threading.Tasks.Task' 到 'System.Linq.IQueryable'

异步GetById存储库方法是:

public async Task<Author> GetById(int id)
{
    return await Task.Run(() => _Authors.Where(a => a.Id == id).SingleOrDefault());
}

如果返回 SingleResult 是让 $expand 为单个实体工作的方法,那么从异步控制器执行此操作的正确方法是什么?

【问题讨论】:

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


    【解决方案1】:

    Get([FromOdataUri]int key) 重命名为 GetAuthor([]int) 可能会解决此问题。
    请参考这篇文章:http://www.asp.net/web-api/overview/odata-support-in-aspnet-web-api/odata-routing-conventions

    【讨论】:

      猜你喜欢
      • 2023-04-08
      • 2013-10-19
      • 2013-07-20
      • 2013-10-08
      • 1970-01-01
      • 2018-10-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多