使用 Refit 进行 OData 调用

Making OData call using Refit

我有如下 OData 查询。

http://localhost:65202/api/odata/StaffBookings?$select=ID&$filter=Staff/ID eq 1&$expand=Staff($select=ID),Event($select=ID,EventDate;$expand=Client($select=ID,Company))

如何使用 Refit 调用它?

谢谢

此致

您可以使用以下OdataParameters class 来设置属性。然后在函数签名中添加 OdataParameters 作为参数。

[Get("/v1/odata/{resource}")]
Task<HttpResponseMessage> GetAdHocDataAsync(
  [Header("Authorization")] string bearerAuthorization,
  string resource,
  OdataParameters odataParams
);

这是 OdataParameters class 您可以根据需要修改

public class OdataParameters
{
    private readonly bool _count;

    public OdataParameters(bool count = false, int? top = null, int? skip = null, string filter = null,
        string select = null, string orderBy = null)
    {
        _count = count;
        Top = top;
        Skip = skip;
        Filter = filter;
        Select = select;
        OrderBy = orderBy;
    }

    [AliasAs("$count")] public string Count => _count ? "true" : null;

    [AliasAs("$top")] public int? Top { get; }

    [AliasAs("$skip")] public int? Skip { get; }

    [AliasAs("$filter")] public string Filter { get; }

    [AliasAs("$select")] public string Select { get; }

    [AliasAs("$orderBy")] public string OrderBy { get; }
}