我正在使用.Net核心的OData Beta,我试图手动应用我的odata过滤器,以便我可以将它们与我的Rest服务基础逻辑一起使用。我可以看到,当我发出请求时,我的控制器被命中但返回正确的数据但由于某种原因我收到错误而不是我的数据。
路线:/api/ODataTest?$filter=not endswith(Name, 'ter')
错误:
URI中指定的查询无效。该物业名称'不能在$ filter查询选项中使用。
这是我的控制器
[Route("api/ODataTest")]
public class ODataTestController : Controller
{
CustomContext _context;
IAdaptable<Skill, SkillDTO> _adapter;
public ODataTestController(CustomContext context, IAdaptable<Skill, SkillDTO> adapter)
{
this._context = context;
this._adapter = adapter;
}
[HttpGet]
[EnableQuery]
public async Task<SkillDTO[]> GetFilteredODataList(ODataQueryOptions<Skill> q)
{
var skillsQuery = this._context.Skills.AsQueryable();
if (q?.Filter != null)
{
skillsQuery = q.Filter.ApplyTo(skillsQuery, new ODataQuerySettings()) as IQueryable<Skill>;
}
var skills = await skillsQuery.Select(s => this._adapter.ToDTO(s)).ToArrayAsync();
return skills;
}
}
我的Startup看起来像这样:
public void ConfigureServices(IServiceCollection services)
{
services.AddOData();
//....
}
public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
{
//...
app.UseMvc(routeBuilder =>
{
routeBuilder.MapODataServiceRoute("odata", null, GetModel());
routeBuilder.EnableDependencyInjection();
});
}
public static IEdmModel GetModel()
{
var builder = new ODataConventionModelBuilder();
var skillSet = builder.EntitySet<Skill>(nameof(Skill));
builder.Namespace = "Astoot.Entities.Models";
builder.ContainerName = "DefaultContainer";
return builder.GetEdmModel();
}
过滤器肯定会检索我的数据,所以为什么我会返回此错误?
答案 0 :(得分:1)
原来我需要在我的RouteBuilder上全局启用过滤
app.UseMvc(routeBuilder =>
{
routeBuilder.Count().Filter().OrderBy().Expand().Select().MaxTop(null);
routeBuilder.MapODataServiceRoute("odata", null, GetModel());
routeBuilder.EnableDependencyInjection();
});