【问题标题】:How to pass DateTimeOffset parameters to oData functions如何将 DateTimeOffset 参数传递给 oData 函数
【发布时间】:2016-01-12 23:02:00
【问题描述】:

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

但我不断收到一条错误消息:

从客户端 (:) 检测到有潜在危险的 Request.Path 值。

问题是日期,好像我这样做了 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 函数,该函数需要一个参数的日期时间偏移量?

【问题讨论】:

    标签: .net function odata asp.net-web-api2 datetimeoffset


    【解决方案1】:

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

    来自 OData 规范:

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

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

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

    https://github.com/OData/WebApi/issues/204 上跟踪了相同的问题

    谢谢。

    【讨论】:

      【解决方案2】:

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

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

      【讨论】:

      • 似乎不太可能,因为 OP 的 filter=ScheduledDate%20 URL 有效,而他们的 GetForPeriod 没有 %s 并且不起作用。请注意,这会影响您的 entire site 的设置。
      • 这个答案是说'%'是无效的......这就是为什么这是OP的潜在解决方案,因为':'已从列表中删除,应该允许时间组件.
      • 今天(2021 年)仍然相关,没想到我之前评论过!
      【解决方案3】:

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

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2015-10-30
        • 1970-01-01
        • 2013-01-27
        • 2021-08-15
        • 2016-05-20
        • 2016-11-17
        相关资源
        最近更新 更多