代码之家  ›  专栏  ›  技术社区  ›  Cade Bryant

c odatacontroller with.net core:odata查询返回空结果集

  •  0
  • Cade Bryant  · 技术社区  · 7 年前

    我在odatacontroller中定义了一个get方法,如下所示:

    [EnableQuery]
    [HttpGet]
    public IActionResult Get(ODataQueryOptions<ActiveDirectoryData> queryOptions)
    {
        var baseQuery = ADQueries.GetADData().AsQueryable();
        return Ok(queryOptions.ApplyTo(baseQuery));
    }
    

    如所料,当我调用不带OData筛选器的URL时,将返回完整的记录集,如下所示:

    http://localhost:64251/odata/ActiveDirectory
    

    下面是我得到的数据的截断示例(基本上是一个数组/IEnumerable对象):

    [{"id":"153d54e3-488f-b5ab-a40c-3c5e9ca0da83","fullyQualifiedName":"CN=DC,CN=Computers,DC=12Sphere,DC=com","dn":["CN=DC,CN=Computers,DC=12Sphere,DC=com"],"objectClass":["computer"],"distinguishedName":["CN=DC,CN=Computers,DC=12Sphere,DC=com"],"whenCreated":["20110418170046.0Z"],"whenChanged":["20180607200521.0Z"],"name":["DC"],"sAMAccountName":["DC$"],"memberOf":[""],"description":[""],"mail":[""],"sn":[""],"givenName":[""],"member":[""],"managedBy":[""]},{"id":"d906e5f4-053c-90b6-bd0f-eacfed15f634","fullyQualifiedName":"CN=EXCH,CN=Computers,DC=12Sphere,DC=com","dn":["CN=EXCH,CN=Computers,DC=12Sphere,DC=com"],"objectClass":["computer"],"distinguishedName":["CN=EXCH,CN=Computers,DC=12Sphere,DC=com"],"whenCreated":["20110418182353.0Z"],"whenChanged":["20180607200521.0Z"],"name":["EXCH"],"sAMAccountName":["EXCH$"],"memberOf":["CN=RAS and IAS Servers,CN=Users,DC=12Sphere,DC=com"],"description":[""],"mail":[""],"sn":[""],"givenName":[""],"member":[""],"managedBy":[""]}
    

    正如我所料。

    但是,当我向url添加odata筛选器时,如下所示:

    http://localhost:64251/odata/ActiveDirectory?$filter=contains(fullyQualifiedName,'Computers')
    

    我什么也没拿回来-只是一张白纸。

    这是我的startup.cs:

    public class Startup
    {
        public Startup(IConfiguration configuration)
        {
            Configuration = configuration;
        }
    
        public IConfiguration Configuration { get; }
    
        // This method gets called by the runtime. Use this method to add services to the container.
        public void ConfigureServices(IServiceCollection services)
        {
            services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_1);
            services.AddOData();
            services.Configure<MongoSettings>(Configuration.GetSection("MongoDB"));
        }
    
        // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
        public void Configure(IApplicationBuilder app, IHostingEnvironment env)
        {
            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
            }
    
            app.UseMvc(builder =>
            {
                builder.Select().Expand().Filter().OrderBy().Count().MaxTop(null);
                builder.MapODataServiceRoute("odata", "odata", GetModel());
                builder.EnableDependencyInjection();
            });
        }
    
        public static IEdmModel GetModel()
        {
            var builder = new ODataConventionModelBuilder();
            builder.EntitySet<ActiveDirectoryData>(nameof(ActiveDirectoryData));
            return builder.GetEdmModel();
        }
    }
    

    我错过了什么?

    顺便说一下,我已经试过删除get方法的[enablequery]属性。我也试过了 具有 [enablequery]但不调用applyto()。不管怎样,我不能让它工作

    0 回复  |  直到 7 年前
    推荐文章