问题描述
OData V4规范规定必须能够: https://issues.oasis-open.org/browse/ODATA-636。"复杂类型和数组只能通过 参数别名"
当我尝试传递具有OData参数别名的数组时,发生异常。
/TestEntities/NS.TestFunction(ArrayHere=@p)?@p=[1,2,3]
结果为:
无法将类型为"EdmValidCoreModelPrimitiveType"的对象强制转换为类型 ‘Microsoft.OData.Edm.IEdm结构类型
有趣的是,元数据文档针对这样的情况进行了正确的组合:
<Function Name="TestFunction" IsBound="true">
<Parameter Name="bindingParameter" Type="Collection(NS.TestEntity)"/>
<Parameter Name="ArrayHere" Type="System.Int32[]"/>
<ReturnType Type="Collection(NS.TestEntity)"/>
</Function>
是否可以使用ASP.NET MVC Web API%2 OData将数组传递给查询字符串中的OData函数?
更新:
以下是构建EDM模型和控制器的代码。
var builder = new ODataConventionModelBuilder();
builder.Namespace = "NS";
builder.EntitySet<TestEntity>("TestEntities");
builder.EntityType<TestEntity>().Collection
.Function("TestFunction")
.ReturnsCollectionFromEntitySet<TestEntity>("TestEntities")
.Parameter<int[]>("ArrayHere");
控制器:
public class TestEntitiesController : ODataController
{
public IEnumerable<TestEntity> TestFunction(int[] arrayHere)
{
throw new NotImplementedException();
}
}
用[FromODataUri]
标记参数不能解决问题。
更新2:
以下是堆栈跟踪:
at Microsoft.OData.Core.UriParser.TypePromotionUtils.CanConvertTo(SingleValueNode sourceNodeOrNull, IEdmTypeReference sourceReference, IEdmTypeReference targetReference)
at Microsoft.OData.Core.UriParser.Parsers.MetadataBindingUtils.ConvertToTypeIfNeeded(SingleValueNode source, IEdmTypeReference targetTypeReference)
at Microsoft.OData.Core.UriParser.Parsers.FunctionCallBinder.BindSegmentParameters(ODataUriParserConfiguration configuration, IEdmOperation functionOrOpertion, ICollection`1 segmentParameterTokens)
at Microsoft.OData.Core.UriParser.Parsers.ODataPathParser.TryBindingParametersAndMatchingOperation(String identifier, String parenthesisExpression, IEdmType bindingType, ODataUriParserConfiguration configuration, ICollection`1& boundParameters, IEdmOperation& matchingOperation)
at Microsoft.OData.Core.UriParser.Parsers.ODataPathParser.TryCreateSegmentForOperation(ODataPathSegment previousSegment, String identifier, String parenthesisExpression)
at Microsoft.OData.Core.UriParser.Parsers.ODataPathParser.CreateNextSegment(String text)
at Microsoft.OData.Core.UriParser.Parsers.ODataPathParser.ParsePath(ICollection`1 segments)
at Microsoft.OData.Core.UriParser.Parsers.ODataPathFactory.BindPath(ICollection`1 segments, ODataUriParserConfiguration configuration)
at Microsoft.OData.Core.UriParser.ODataUriParser.ParsePathImplementation()
at Microsoft.OData.Core.UriParser.ODataUriParser.Initialize()
at System.Web.OData.Routing.DefaultODataPathHandler.Parse(IEdmModel model, String serviceRoot, String odataPath, Boolean enableUriTemplateParsing)
at System.Web.OData.Routing.DefaultODataPathHandler.Parse(IEdmModel model, String serviceRoot, String odataPath)
at System.Web.OData.Routing.ODataPathRouteConstraint.Match(HttpRequestMessage request, IHttpRoute route, String parameterName, IDictionary`2 values, HttpRouteDirection routeDirection)
at System.Web.Http.Routing.HttpRoute.ProcessConstraint(HttpRequestMessage request, Object constraint, String parameterName, HttpRouteValueDictionary values, HttpRouteDirection routeDirection)
at System.Web.Http.Routing.HttpRoute.ProcessConstraints(HttpRequestMessage request, HttpRouteValueDictionary values, HttpRouteDirection routeDirection)
at System.Web.Http.Routing.HttpRoute.GetRouteData(String virtualPathRoot, HttpRequestMessage request)
at System.Web.Http.WebHost.Routing.HttpWebRoute.GetRouteData(HttpContextBase httpContext)
推荐答案
假设您使用的是OData V4,则在注册函数时需要使用CollectionParameter
,并且在arrayHere参数上缺少[FromODataUri]
。此外,请尝试使用IEnumerable<int>
而不是数组。
var builder = new ODataConventionModelBuilder();
builder.Namespace = "NS";
builder.EntitySet<TestEntity>("TestEntities");
builder.EntityType<TestEntity>().Collection
.Function("TestFunction")
.ReturnsCollectionFromEntitySet<TestEntity>("TestEntities")
.CollectionParameter<int>("ArrayHere");
使用控制器中的函数如下.
[HttpGet]
public async Task<IHttpActionResult> TestFunction([FromODataUri] IEnumerable<int> ArrayHere)
{
// Do stuff
}
现在您可以提出如下请求.
http://yourRestService/API/TestEntities/NS.TestFunction(ArrayHere=[1,2,3])
builder.EntityType<TestEntity>().Collection
.Function("TestFunction2")
.ReturnsCollectionFromEntitySet<TestEntity>("TestEntities")
.CollectionParameter<person>("ArrayHere");
和
[HttpGet]
public async Task<IHttpActionResult> TestFunction2([FromODataUri] IEnumerable<person> ArrayHere)
{
// Do stuff
}
http://yourRestService/API/TestEntities/NS.TestFunction2(ArrayHere=@ArrayData)?@ArrayData=%5B%7B%22FirstName%22%3A%22Bob%22%2C+%22LastName%22%3A%22Dole%22%7D%2C%7B%22FirstName%22%3A%22Bill%22%2C+%22LastName%22%3A%22Clinton%22%7D%5D
这篇关于如何在ASP.NET Web API实现中将Array传递给OData函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!