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

c# – ASP.NET MVC Web API并传递oData查询,第1张

概述我目前正在使用oData过滤器请求执行Web API,如下所示: public IQueryable<OrganizationViewModel> Get(ODataQueryOptions<Organization> oDataQuery){ var query = new FindOrganizationsQuery(oDataQuery); var result =_fin 我目前正在使用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过滤器?

解决方法 您可以为我们为ODataqueryOptions执行的 same way的FindOrganizationsquery编写自定义参数绑定属性,然后使用该属性对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;    }}

我有完整的样品here.

总结

以上是内存溢出为你收集整理的c# – ASP.NET MVC Web API并传递oData查询全部内容,希望文章能够帮你解决c# – ASP.NET MVC Web API并传递oData查询所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/langs/1230090.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-06-06
下一篇 2022-06-06

发表评论

登录后才能评论

评论列表(0条)

保存