ASP.NET MVC Web API并传递oData查询

时间:2013-08-15 09:48:30

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

我目前正在使用oData过滤器请求执行Web API,如下所示:

public IQueryable<OrganizationViewModel> Get(ODataQueryOptions<Organization> oDataQuery)
{
    var query = new FindOrganizationsQuery(oDataQuery);
    var result =_findOrganizationsQueryHandler.Execute(query);
    return result.Organizations.Select(o => new OrganizationViewModel { Id = o.PublicId, Name = o.Name });
}

处理程序如下:

public FindOrganizationsQueryResult Execute(FindOrganizationsQuery request)
{
    var organizations = request.ODataQuery.ApplyTo(_mgpQueryContext.Organizations).Cast<Organization>();            
    return new FindOrganizationsQueryResult(organizations);
}

查询类看起来像:

public class FindOrganizationsQuery
{
    public FindOrganizationsQuery(ODataQueryOptions<Organization> oDataQuery)
    {
        ODataQuery = oDataQuery;
    }
    public ODataQueryOptions<Organization> ODataQuery { get; set; }
}

因此,如果我使用请求传递oData过滤器,则处理得很好,这一切都正常。

但是现在,我不想将类型ODataQueryOptions传递给Get操作,而是希望有FindOrganizationsQuery类,如:

public IQueryable<OrganizationViewModel> FindOrganizations(FindOrganizationsQuery query)
{
    // query is null
}

但是,查询参数始终为null。如果ODataQueryOptions参数在另一个类中,我如何传递oData过滤器?

1 个答案:

答案 0 :(得分:1)

您可以为我FindOrganizationsQuery的{​​{1}} ODataQueryOptions编写自定义参数绑定属性,然后将FindOrganizationsQuery与该属性相关联。

下面的一些示例代码,

public class CustomQueryBindingAttribute : ParameterBindingAttribute
{
    public override HttpParameterBinding GetBinding(HttpParameterDescriptor parameter)
    {
        return new CustomQueryBinding(parameter);
    }

    internal class CustomQueryBinding : HttpParameterBinding
    {
        public CustomQueryBinding(HttpParameterDescriptor parameter)
            : base(parameter)
        {
        }

    internal class CustomQueryBinding : HttpParameterBinding
    {
        public CustomQueryBinding(HttpParameterDescriptor parameter)
            : base(parameter)
        {
        }

        public override Task ExecuteBindingAsync(ModelMetadataProvider metadataProvider,
            HttpActionContext actionContext, CancellationToken cancellationToken)
        {
            IEdmModel model = actionContext.Request.GetEdmModel() ?? actionContext.ActionDescriptor.GetEdmModel(typeof(Organization));
            ODataQueryContext queryContext = new ODataQueryContext(model, typeof(Organization));

            object customQuery = CreateCustomQuery(queryContext, actionContext.Request);
            SetValue(actionContext, customQuery);

            return Task.FromResult(0);
        }

        private object CreateCustomQuery(ODataQueryContext queryContext, HttpRequestMessage request)
        {
            Type parameterType = Descriptor.ParameterType;
            // Assuming all custom queries have this public property.
            Type oDataQueryOptionsOfTType = parameterType.GetProperty("ODataQuery").PropertyType;

            object odataQueryOptions = Activator.CreateInstance(oDataQueryOptionsOfTType, queryContext, request);
            return Activator.CreateInstance(parameterType, odataQueryOptions);
        }
    }
}

以及我从Web API源代码复制的扩展方法,因为它不公开。

public static class HttpActionDescriptorExtensions
{
    internal const string EdmModelKey = "MS_EdmModel";

    internal static IEdmModel GetEdmModel(this HttpActionDescriptor actionDescriptor, Type entityClrType)
    {
        // save the EdmModel to the action descriptor
        return actionDescriptor.Properties.GetOrAdd(EdmModelKey + entityClrType.FullName, _ =>
        {
            ODataConventionModelBuilder builder = new ODataConventionModelBuilder(actionDescriptor.Configuration, isQueryCompositionMode: true);
            EntityTypeConfiguration entityTypeConfiguration = builder.AddEntity(entityClrType);
            builder.AddEntitySet(entityClrType.Name, entityTypeConfiguration);
            IEdmModel edmModel = builder.GetEdmModel();
            return edmModel;
        }) as IEdmModel;
    }
}

我有完整的样本same way