简体   繁体   English

使用TimeSpan的Web API OData功能

[英]Web API OData function with TimeSpan

I have a Web API OData v4 service that I've created. 我有一个我创建的Web API OData v4服务。 I'm trying to create a bound function that has a TimeSpan parameter having a signature defined in the OData controller similar to the below: 我正在尝试创建一个绑定函数,该函数具有TimeSpan参数,该参数具有在OData控制器中定义的签名,类似于以下内容:

public IQueryable<ProductionRecordDTO> GetProduction(
        [FromODataUri]DateTimeOffset startDate,
        [FromODataUri]DateTimeOffset endDate,
        [FromODataUri]TimeSpan sampleInterval)

It is configured in the OData model builder as below: 它在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");

When run, the model is seemingly properly created, the metadata description show that the "SampleInterval" is properly defined as an Edm.Duration type. 运行时,模型看似正确创建,元数据描述显示“SampleInterval”已正确定义为Edm.Duration类型。

When I attempt to call this method however with a URL such as: 当我尝试使用以下URL调用此方法时:

http://dev-pc/odata/Products/ProductsService.GetProduction(StartDate=2014-01-01T00:00:00Z, EndDate=2017-01-01T00:00:00Z, SampleInterval=P1Y)

An ODataException is thrown with the message 'SampleInterval=P1Y' is not in scope. 抛出ODataException,并且消息“SampleInterval = P1Y”不在范围内。 The same is true for every ISO 8601 duration format variation I give it. 对于我给出的每个ISO 8601持续时间格式变化,情况也是如此。

Using: 使用:

  • Microsoft.OData.Core - v6.15.0 Microsoft.OData.Core - v6.15.0
  • Microsoft.AspNet.OData - v5.9.1 Microsoft.AspNet.OData - v5.9.1

Any assistance offered would be greatly appreciated. 任何提供的援助将不胜感激。

I found the cause. 我找到了原因。 Parameters of Edm.Duration apparently cannot be interpreted litterally and need to have a type wrapped around them, eg duration'P1D' In this case a correct call would have been: Edm.Duration的参数显然不能在文字上解释,并且需要在它们周围缠绕一个类型,例如duration'P1D'在这种情况下,正确的调用将是:

http://dev-pc/odata/Products/ProductsService.GetProduction(StartDate=2014-01-01T00:00:00Z, EndDate=2017-01-01T00:00:00Z, SampleInterval=duration'P1D')

That said, Microsoft's implementation doesn't seem to accept periodic kinds larger than days. 也就是说,微软的实施似乎并不接受大于天的定期种类。 P1W , P1M and P1Y are all rejected. P1WP1MP1Y全部被拒绝。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM