AspNetCore.OData 7.1.0 "$ search" опция запроса не работает - PullRequest
0 голосов
/ 11 января 2019

Я создал .Net Core 2 API с использованием OData 4.0 (часть AspNetCore.OData 7.1.0). Все, кроме "$ search", похоже, работает. Документация говорит, что она должна работать.

Следующие запросы, которые я проверял, не работали:

сообщение об ошибке:

{"message":"The query parameter 'Specified argument was out of the range of valid values.\r\nParameter name: $search' is not supported.","exceptionMessage":"Specified argument was out of the range of valid values.\r\nParameter name: $search","exceptionType":"System.ArgumentOutOfRangeException","stackTrace":"   at Microsoft.AspNet.OData.EnableQueryAttribute.ValidateQuery(HttpRequest request, ODataQueryOptions queryOptions)\r\n   at Microsoft.AspNet.OData.EnableQueryAttribute.<>c__DisplayClass1_0.<OnActionExecuted>b__3(ODataQueryContext queryContext)\r\n   at Microsoft.AspNet.OData.EnableQueryAttribute.ExecuteQuery(Object responseValue, IQueryable singleResultCollection, IWebApiActionDescriptor actionDescriptor, Func`2 modelFunction, IWebApiRequestMessage request, Func`2 createQueryOptionFunction)\r\n   at Microsoft.AspNet.OData.EnableQueryAttribute.OnActionExecuted(Object responseValue, IQueryable singleResultCollection, IWebApiActionDescriptor actionDescriptor, IWebApiRequestMessage request, Func`2 modelFunction, Func`2 createQueryOptionFunction, Action`1 createResponseAction, Action`3 createErrorAction)"}

Следующие протестированные мной запросы работали:

Мой код:

Настроить приложение (определено в Startup.cs):

    public void Configure(IApplicationBuilder app, IHostingEnvironment env)
    {
        if (env.IsDevelopment())
        {
            app.UseDeveloperExceptionPage();
        }
        else
        {
            // The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
            app.UseHsts();
        }

        // Cors
        app.UseCors(builder => builder
            .WithOrigins("*")
            .AllowAnyHeader()
            .AllowAnyOrigin()
            .AllowAnyMethod()
            .AllowCredentials()
        );

        //app.UseHttpsRedirection();
        app.UseMvc(routeBuilder =>
        {
            routeBuilder.MapODataServiceRoute("odata", $"service/", GetEdmModel());
            routeBuilder.Count().Filter().OrderBy().Expand().Select().MaxTop(null);
            routeBuilder.EnableDependencyInjection();
        });
    }

EdmModel (определено в Startup.cs):

    private static IEdmModel GetEdmModel()
    {
        ODataConventionModelBuilder builder = new ODataConventionModelBuilder();
        builder.EntitySet<Product>("Product");
        builder.EntitySet<Producer>("Producer");
        builder.EntitySet<Consumer>("Consumer");
        return builder.GetEdmModel();
    }

.Net Core 2.2 Api "Получить" (ProductsController.cs):

    [ProducesResponseType((int)HttpStatusCode.OK, Type = typeof(IEnumerable<Product>))]
    [ProducesResponseType((int)HttpStatusCode.BadRequest)]
    [EnableQuery]
    public async Task<ActionResult<Product>> Get()
    {
        var dbResponse = _context.Products.AsQueryable();
        return this.OK(dbresponse);
    }
...