带有“和”运算符的OData过滤器无法在Web Api中运行

问题描述 投票:-1回答:1

我正在使用单个GET操作创建一个Web Api,它处理下面的get请求。

获取https://localhost:44378/v1/RoutePrefix/Route?$ filter = Id eq'1234'$ select = Name(这个工作正常)

获取https://localhost:44378/v1/RoutePrefix/Route?$ filter = Id eq'1234'或MessageType eq'1'(此工作正常)

获取https://localhost:44378/v1/RoutePrefix/Route?$ filter = Id eq'1234'和MessageType eq'1'(这个没有用。响应值总是[])

看起来像“和”运算符的过滤器不起作用。 “或”运算符对我来说很好。

我在webapiconfig.cs中有以下代码。

public static class WebApiConfig
{
    public static void Register(HttpConfiguration config)
    {
        config.MapHttpAttributeRoutes();
        config.MapODataServiceRoute("odata", "v1/RoutePrefix", GetEdmModel(), new DefaultODataBatchHandler(GlobalConfiguration.DefaultServer));
        config.Count().Filter().OrderBy().Expand().Select().MaxTop(null);
    }
    private static IEdmModel GetEdmModel()
    {
        ODataConventionModelBuilder builder = new ODataConventionModelBuilder();
        builder.Namespace = "Default";
        builder.ContainerName = "DefaultContainer";
        builder.EntitySet<Model>("Route");
        builder.EntitySet<Model>("Route").EntityType.Filter(nameof(Model.Id));
        builder.EntitySet<Model>("Route").EntityType.Filter(nameof(Model.MessageType));
        var edmModel = builder.GetEdmModel();

        return edmModel;
    }
}

在我的控制器中,根据滤波器参数的数量,我调用不同的方法。两种方法都返回List作为对GET方法的响应。在Main get方法中,我返回Ok(List.AsQueryable())。

我使用[EnableQuery]属性和Implemented ODataController修饰控制器,如下所示:

[EnableQuery] public class RouteController:ODataController

并且Get方法如下所示:

public IHttpActionResult Get()
{
        List<Model> response = null;
        string cacheKey = string.Empty;
        var queryString = Request.RequestUri.PathAndQuery.Replace("/v1/RoutePrefix", "");
        var queryTokens = ParseQueryString(EdmModel, queryString);

        if (queryTokens == null || queryTokens.Any(a => string.IsNullOrWhiteSpace(a.Value)) || !queryTokens.ContainsKey(Constants.Id))
        {
            IList<ApiError> errors = new List<ApiError>() { new ApiError(Constants.InvalidQueryStringErrorCode, Constants.InvalidQueryStringErrorMessage) };
            return GenerateResponse(Request, HttpStatusCode.BadRequest, errors, null);
        }
        else
        {
            try
            {
                if (queryTokens.ContainsKey(Constants.MessageType))
                {
                    response = GetConfigurationByMessageTypeAndId(queryTokens);
                }
                else
                {
                    response = GetConfigurationById(queryTokens);
                }
            }
            catch (Exception ex)
            {
                var apiError = Utilities.CreateApiError(Constants.InternalServerError, Constants.InternalServerErrorMessage, null, null, null);
                IList<ApiError> apiErrors = new List<ApiError> { apiError };

                return GenerateResponse(Request, HttpStatusCode.InternalServerError, apiErrors, null);
            }

            if (response.Count > 0)
            {
                return Ok(response.AsQueryable());
            }
            else
            {
                return NotFound();
            }
        }
    }

请让我知道我做错了什么。

谢谢

c# azure asp.net-web-api2 odata odata-v4
1个回答
0
投票

问题已解决。$ filter system query选项允许客户端过滤请求URL寻址的资源集合。使用$ filter指定的表达式将针对集合中的每个资源进行求值,并且只有表达式求值为true的项才会包含在响应中。响应中省略了表达式求值为false或为null的资源,或者由于权限而不可用的引用属性。我的最终集合没有在查询字符串中传递的原始值。它被不同的价值所取代 - Kumaraguru 1分钟前编辑

© www.soinside.com 2019 - 2024. All rights reserved.