如何使用ASP.NET Web Api将对象作为参数传递给OData Action?

编程入门 行业动态 更新时间:2024-10-17 11:24:31
本文介绍了如何使用ASP.NET Web Api将对象作为参数传递给OData Action?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

如何将对象(实体)传递给OData操作?我可以传递简单类型和ComplexType,但是如果尝试传递EntityType,我会得到:

How can I pass an object (Entity) to an OData Action? I can pass simple types and ComplexType, but if try to pass an EntityType I get:

Invalid parameter type 'apitest.Models.Product'. A non-binding parameter type must be either Primitive, Complex, Collection of Primitive or a Collection of Complex.

var modelBuilder = new ODataConventionModelBuilder(); modelBuilder.EntitySet<Product>("Products"); modelBuilder.EntitySet<PurchaseOrder>("PurchaseOrders"); var addProduct = modelBuilder.Entity<PurchaseOrder>().Action("AddProduct"); addProduct.Parameter<Product>("product"); addProduct.ReturnsFromEntitySet<PurchaseOrder>("PurchaseOrders");

我尝试将addProduct.Parameter<Product>("product");替换为:

var productConfig = modelBuilder.StructuralTypes.OfType<EntityTypeConfiguration>().Single(x => x.Name == "Product"); addProduct.SetBindingParameter("product", productConfig, false);

但是我还找不到任何有关如何使用SetBindingParameter的文档.如果我将Product注册为ComplexType,则第一个代码示例有效,但是之后我将其注册为Entity.

But I haven't been able to find any documentation on how to use SetBindingParameter. If I register Product as a ComplexType, the first code sample works, but then I'm not allowed to register it as Entity.

推荐答案

假设您将操作绑定到实体类型,则可以使用

Assuming that you bind an action to an Entity type this works

builder.EntityType<company>().Action("SetCommunities").CollectionEntityParameter<community>("communities"); public async Task<IHttpActionResult> SetCommunities(Guid key, ODataActionParameters parameters)

在这种情况下,例如 localhost:63358/odata/公司(451735ef-15f2-4a9f-b4b9-d50c6410bf7c)/Default.SetCommunities/

更多推荐

如何使用ASP.NET Web Api将对象作为参数传递给OData Action?

本文发布于:2023-11-14 04:12:06,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1586181.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:如何使用   对象   参数   NET   ASP

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!