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

前端之家收集整理的这篇文章主要介绍了c# – 如何将过滤器选项从ODataQueryOptions映射到RestRequest前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我需要能够从 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进行调用,我将以完全相同的方式执行此操作.

原文链接:https://www.f2er.com/csharp/239100.html

猜你在找的C#相关文章