加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

c# – 如何将过滤器选项从ODataQueryOptions映射到RestRequest

发布时间:2020-12-15 08:15:02 所属栏目:百科 来源:网络整理
导读:我需要能够从 ODataQueryOptions转换到 RestRequest,以便能够使用指定的过滤器发出RestRequest,并创建了以下帮助程序类: public static class ODataQueryFilterToRestClient{ public static RestRequest Map(ODataQueryOptions odataQuery) { var restReque
我需要能够从 ODataQueryOptions转换到 RestRequest,以便能够使用指定的过滤器发出RestRequest,并创建了以下帮助程序类:
public static class ODataQueryFilterToRestClient
{
    public static RestRequest Map(ODataQueryOptions odataQuery)
    {
        var restRequest = new RestRequest();

        if (odataQuery.Filter != null)
        {
            restRequest.AddQueryParameter(@"$filter",odataQuery.Filter.RawValue);
        }

        if (odataQuery.Top != null)
        {
            restRequest.AddQueryParameter(@"$top",odataQuery.Top.RawValue);
        }

        if (odataQuery.Skip != null)
        {
            restRequest.AddQueryParameter(@"$skip",odataQuery.Skip.RawValue);
        }

        if (odataQuery.OrderBy != null)
        {
            restRequest.AddQueryParameter(@"$orderby",odataQuery.OrderBy.RawValue);
        }
        //etc
        return restRequest;
    }
}

鉴于OdataQueryOptions支持以下内容:

有没有更简单的方法在ODataQueryOptions与RestClient或其他其他客户端代理之间进行转换?

另外,我不知道是否有更好的方法通过控制器接受参数而不是ODataQueryOptions?

解决方法

RestSharp中没有ODataQueryOptions的直接支持.

还有其他专门用于查询OData的客户端,例如: Simple.OData.Client.但是,对于请求,它也不能与ODataQueryOptions一起运行,从而提供流畅的API.

总体而言,ODataQueryOptions在OData兼容的RESTful API中用于服务器端.客户端(包括RestSharp)只使用其常规语法来提供请求数据.

所以回答你的问题(有更简单的方法……) – 不,没有.

但是,您的转换方法看起来很简单.如果我必须使用RestSharp与给定的ODataQueryOptions进行调用,我将以完全相同的方式执行此操作.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读