带有.NET Core的C#ODataController:OData查询返回空结果集

时间:2018-12-10 21:05:39

标签: c# asp.net-web-api .net-core odata

我在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 个答案:

没有答案