我正在尝试实现接收两个 DateTimeOffset 的 OData 集合函数?参数(MinSentOn 和 MaxSentOn)并将从订单表返回一些摘要信息,但是当我传递 DateTimeOffset 的时间部分时遇到路由问题,收到 HTTP 错误 500.0 - 内部服务器错误直接从 IIS,因为它似乎试图访问文件而不是 Controller 本身。
这是我当前的 OData 配置:
odataBuilder.Namespace = "D";
var fc =
odataBuilder.EntityType<Order>().Collection
.Function("ToExecutiveSummary")
.Returns<ExecutiveSummary>();
fc.Parameter<DateTimeOffset?>("MinSentOn");
fc.Parameter<DateTimeOffset?>("MaxSentOn");
这是我 Controller 中的函数:
[HttpGet]
public async Task<IHttpActionResult> ToExecutiveSummary(DateTimeOffset? minSentOn, DateTimeOffset? maxSentOn, CancellationToken ct)
{
return await _uow.ExecuteAndCommitAsync(async () =>
{
var query = _uow.Orders.Query();
if (minSentOn != null) query = query.Where(e => e.SentOn >= minSentOn.Value);
if (maxSentOn != null) query = query.Where(e => e.SentOn <= maxSentOn.Value);
// TODO needs optimization, test only
var executiveSummary =
query.Select(e =>
new ExecutiveSummary
{
TotalOrders = query.Count(),
TotalProducts = query.Sum(ex => ex.Quantity),
TotalPharmacies = query.GroupBy(ex => ex.Pharmacy.Id, ex => ex.Pharmacy.Id).Count()
}).FirstOrDefault();
return Ok(executiveSummary);
}, ct);
}
web.config 的片段更改以支持 OData 路径并解决我遇到这堵墙之前遇到的一些路由问题,例如点或双重转义(更改有注释):
<configuration>
<!-- ... -->
<system.web>
<compilation debug="true" targetFramework="4.5.2" />
<!-- Removed : and % from the path filter -->
<httpRuntime targetFramework="4.5.2" requestPathInvalidCharacters="<,>,*,&,\,?"/>
<globalization uiCulture="pt-PT" culture="pt-PT" />
</system.web>
<!-- ... -->
<system.webServer>
<handlers>
<remove name="ExtensionlessUrlHandler-Integrated-4.0" />
<remove name="OPTIONSVerbHandler" />
<remove name="TRACEVerbHandler" />
<!-- to support the dot (.) for functions or actions -->
<add name="ExtensionlessUrlHandler-Integrated-4.0" path="/*" verb="*" type="System.Web.Handlers.TransferRequestHandler" preCondition="integratedMode,runtimeVersionv4.0" />
</handlers>
<security>
<!-- to support double escapings, like 2015-08-10%2000:00:00.0000%2B01:00 -->
<requestFiltering allowDoubleEscaping="true"/>
</security>
</system.webServer>
<!-- ... -->
</configuration>
现在,在我的测试过程中,我面临以下问题:
如果我没有超过时间部分(例如:http://localhost:58806/odata/Order/D.ToExecutiveSummary(MinSentOn=null,MaxSentOn=2015-08-10)),请求将毫无问题地到达我的代码,这让我相信 OData 配置和路由没有任何问题。 但是当我包括时间部分(例如:http://localhost:58806/odata/Order/D.ToExecutiveSummary(MinSentOn=null,MaxSentOn=2015-08-10%2000:00:00.0000%2B01:00))时,我直接从 IIS 收到内部服务器错误(附图像)。它似乎正在尝试解析为文件而不是 Controller ,问题由此而来。
最终,我知道我可以接收字符串形式的参数并自己进行解析,但我想在不使用“锤子”的情况下实现它:)
最佳答案
我认为您可以为 DateTimeOffset 参数值使用参数别名。
例如:
在下面的Uri中,可以找到很多参数别名的例子: https://github.com/OData/WebApi/blob/master/OData/test/UnitTest/System.Web.OData.Test/OData/Formatter/ODataFunctionTests.cs#L24-L43
此外,在 http://odata.github.io/WebApi/#04-06-function-parameter-support ,您可以找到有关功能参数的简单指南。
但是,该问题是与 IIS 相关的已知问题,可在 odata Web APi@github 上进行跟踪
谢谢。
关于c# - 带有 DateTimeOffset 的 OData 函数?范围,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32501253/