如何将DateTimeOffset参数传递给oData函数

3

我有一个名为 GetForPeriod 的OData函数,定义如下:

        var getForPeriod =
            builder.EntityType<EventModel>()
                .Collection
                .Function("GetForPeriod")
                .ReturnsCollection<EventModelSummary>();
        getForPeriod.Parameter<DateTimeOffset>("from");
        getForPeriod.Parameter<DateTimeOffset>("to");

为了从这个函数中获得结果,我需要调用以下内容:http://localhost:17257/odata/Events/Default.GetForPeriod(from=2015-12-27T00:00:00-06:00,to=2016-02-06T00:00:00-06:00)。但是我一直收到一个错误提示:

从客户端(:)检测到一个潜在危险的请求路径值。

问题出在日期上,如果我使用http://localhost:17257/odata/Events/Default.GetForPeriod(from=null,to=null),我会收到一个错误,指出无法将空值转换为DateTimeOffset(这很有道理)。我尝试将from和to中的冒号(:)替换为%3A,但我仍然收到相同的“危险路径”错误。有趣的是,如果我调用带有日期过滤器的事件读取路径,它就可以正常工作:http://localhost:17257/odata/Events?$filter=ScheduledDate%20ge%202015-12-27T00:00:00-06:00%20and%20ScheduledDate%20le%202016-02-06T00:00:00-06:00。那么我该如何调用需要DateTimeOffset参数的OData函数呢?
3个回答

3

请尝试使用函数参数别名?

根据OData规范:

参数别名可以用来替代函数调用中的内联参数。别名的值可以通过使用别名参数的名称作为单独的查询选项进行指定。

示例76:通过调用函数导入EmployeesByManager,将3传递给ManagerID参数来调用Sales.EmployeesByManager函数。

http://host/service/EmployeesByManager(ManagerID=@p1)?@p1=3

同样的问题在https://github.com/OData/WebApi/issues/204上有记录。

谢谢。


1
将以下内容添加到您的 web.config 文件中。
<system.web>
    <httpRuntime requestPathInvalidCharacters="%" />
</system.web>

似乎不太可能,因为原帖中的filter=ScheduledDate%20 URL 可以工作,而他们的GetForPeriod没有%s并且无法工作。请注意,这会影响您整个站点的设置。 - Noumenon
这个答案表明'%'是无效的...这就是为什么这是一个潜在的解决方案给OP的原因,因为':'已经从列表中删除,时间组件应该被允许。 - Chris Schaller
今天(2021年)仍然相关,之前没有意识到我已经发表了评论! - Chris Schaller

0
我需要创建一个名为web.config的文件,并将以下内容添加进去:
<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <system.webServer>
    <security>
      <requestFiltering allowDoubleEscaping="true"/>
    </security>
  </system.webServer>
</configuration>

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接