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查询所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)