我有我创建的Web API ODATA V4服务。我正在尝试创建一个具有timespan参数的界限函数,该参数在ODATA控制器中定义了类似于以下的签名:
public IQueryable<ProductionRecordDTO> GetProduction(
[FromODataUri]DateTimeOffset startDate,
[FromODataUri]DateTimeOffset endDate,
[FromODataUri]TimeSpan sampleInterval)
它在ODATA模型构建器中配置如下:
var getProduction = builder.EntitySet<ProductDTO>("Products").EntityType.Collection.Function("GetProduction");
getProduction.Namespace = "ProductsService";
getProduction.ReturnsCollection<ProductionRecordDTO>();
getProduction.Parameter<DateTimeOffset>("StartDate");
getProduction.Parameter<DateTimeOffset>("EndDate");
getProduction.Parameter<TimeSpan>("SampleInterval");
运行时,模型似乎是正确创建的,元数据描述表明" sample Interval"被正确定义为EDM.Duration类型。
当我尝试使用一个URL调用此方法时,例如:
http://dev-pc/odata/Products/ProductsService.GetProduction(StartDate=2014-01-01T00:00:00Z, EndDate=2017-01-01T00:00:00Z, SampleInterval=P1Y)
odataException被带有消息" sampleinterval = p1y"的消息不在范围中。我给它的每个ISO 8601持续时间格式变化都是如此。
使用:
- microsoft.odata.core -v6.15.0
- Microsoft.aspnet.odata -V5.9.1
提供的任何帮助将不胜感激。
我找到了原因。EDM的参数显然无法用刻度解释,需要包裹在它们周围的类型,例如duration'P1D'
在这种情况下,正确的呼叫将是:
http://dev-pc/odata/Products/ProductsService.GetProduction(StartDate=2014-01-01T00:00:00Z, EndDate=2017-01-01T00:00:00Z, SampleInterval=duration'P1D')
也就是说,微软的实施似乎并不接受比天大的定期类型。P1W
,P1M
和P1Y
都被拒绝。