我使用OData 4.0(AspNetCore.OData 7.1.0的一部分)创建了.Net Core 2 API。 除“ $ search”外,其他所有功能似乎都可以使用。 documentation说应该可以。
我测试的以下请求无效:
错误消息:
{"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);
}