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

How to pass DateTimeOffset parameters to oData functions

我有一个名为 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)

但我不断收到错误消息:

A potentially dangerous Request.Path value was detected from the client (:).

问题是日期,好像我有 http://localhost:17257/odata/Events/Default.GetForPeriod(from=null,to=null) 我收到一条错误消息,指出它无法将 null 转换为 DateTimeOffset(这是有道理的)。

我尝试将 from 中的冒号 (:) 和两个值替换为 %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

我应该如何调用 OData 函数,该函数采用参数的日期时间偏移量?

请你试试函数参数别名好吗?

来自 OData 规范:

可以使用参数别名代替函数调用的内联参数。别名的值使用参数别名的名称指定为单独的查询选项。

示例 76:通过函数 import EmployeesByManager 调用 Sales.EmployeesByManager 函数,为 ManagerID 参数传递 3

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

https://github.com/OData/WebApi/issues/204

上跟踪了相同的问题

谢谢。

将此添加到您的 web.config 文件中。

<system.web>
    <httpRuntime requestPathInvalidCharacters="%" />
</system.web>

我必须创建一个包含以下内容的 web.config 文件:

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <system.webServer>
    <security>
      <requestFiltering allowDoubleEscaping="true"/>
    </security>
  </system.webServer>
</configuration>