typescript 按日期筛选的OData抛出“URI中指定的查询无效,位置62处应包含标识符”

zphenhs4  于 2023-01-27  发布在  TypeScript
关注(0)|答案(3)|浏览(220)

我正在使用OData查询SQL数据库中的数据,我正在尝试使用日期值筛选数据。请参阅下面的查询:

http://localhost:4409/OrderMaintenance?$filter=DocType%20eq%205%20and%20DocState%20in%20(0,1,2,3,4,5,6,7)%20and%20InvDate%20eq%202022/02/21

此查询中只有日期有问题,因为当我删除日期筛选器时,查询工作正常。
InvDate的类型为:

orderDateFilter: Date = new Date();

然后,我将日期格式设置为:

this.orderDateFilter.toLocaleDateString()

返回日期为:

2022/02/21

但我收到以下错误

"The query specified in the URI is not valid. An identifier was expected at position 62."

堆栈跟踪:

at Microsoft.OData.UriParser.ExpressionToken.GetIdentifier()\r\n   at Microsoft.OData.UriParser.UriQueryExpressionParser.ParseSegment(QueryToken parent)\r\n   at Microsoft.OData.UriParser.UriQueryExpressionParser.ParsePrimary()\r\n   at Microsoft.OData.UriParser.UriQueryExpressionParser.ParseInHas()\r\n   at Microsoft.OData.UriParser.UriQueryExpressionParser.ParseUnary()\r\n   at Microsoft.OData.UriParser.UriQueryExpressionParser.ParseMultiplicative()\r\n   at Microsoft.OData.UriParser.UriQueryExpressionParser.ParseAdditive()\r\n   at Microsoft.OData.UriParser.UriQueryExpressionParser.ParseComparison()\r\n   at Microsoft.OData.UriParser.UriQueryExpressionParser.ParseLogicalAnd()\r\n   at Microsoft.OData.UriParser.UriQueryExpressionParser.ParseLogicalOr()\r\n   at Microsoft.OData.UriParser.UriQueryExpressionParser.ParseExpression()\r\n   at Microsoft.OData.UriParser.UriQueryExpressionParser.ParseExpressionText(String expressionText)\r\n   at Microsoft.OData.UriParser.ODataQueryOptionParser.ParseFilterImplementation(String filter, ODataUriParserConfiguration configuration, ODataPathInfo odataPathInfo)\r\n   at Microsoft.OData.UriParser.ODataQueryOptionParser.ParseFilter()\r\n   at Microsoft.AspNet.OData.Query.FilterQueryOption.get_FilterClause()\r\n   at Microsoft.AspNet.OData.Query.Validators.FilterQueryValidator.Validate(FilterQueryOption filterQueryOption, ODataValidationSettings settings)\r\n   at Microsoft.AspNet.OData.Query.FilterQueryOption.Validate(ODataValidationSettings validationSettings)\r\n   at Microsoft.AspNet.OData.Query.Validators.ODataQueryValidator.Validate(ODataQueryOptions options, ODataValidationSettings validationSettings)\r\n   at Microsoft.AspNet.OData.Query.ODataQueryOptions.Validate(ODataValidationSettings validationSettings)\r\n   at Microsoft.AspNet.OData.EnableQueryAttribute.ValidateQuery(HttpRequestMessage request, ODataQueryOptions queryOptions)\r\n   at Microsoft.AspNet.OData.EnableQueryAttribute.<>c__DisplayClass0_0.<OnActionExecuted>b__1(ODataQueryContext queryContext)\r\n   at Microsoft.AspNet.OData.EnableQueryAttribute.ExecuteQuery(Object responseValue, IQueryable singleResultCollection, IWebApiActionDescriptor actionDescriptor, Func`2 modelFunction, IWebApiRequestMessage request, Func`2 createQueryOptionFunction)\r\n   at Microsoft.AspNet.OData.EnableQueryAttribute.OnActionExecuted(Object responseValue, IQueryable singleResultCollection, IWebApiActionDescriptor actionDescriptor, IWebApiRequestMessage request, Func`2 modelFunction, Func`2 createQueryOptionFunction, Action`1 createResponseAction, Action`3 createErrorAction)
rxztt3cl

rxztt3cl1#

尝试使用破折号而不是反斜杠来格式化odata日期过滤器:2022-02-21而不是2022 - 02 - 21。您可能还需要添加时间部分。以下是时间为2022-02- 21 T00:00:00 Z午夜的示例。

xoefb8l8

xoefb8l82#

我使用了:

this.orderDateFilter.toISOString()

将我的日期转换为:

2022-02-22T09:17:24.822Z

但是我在查询时遇到了问题,因为我只想检查日期而不想检查时间。所以我使用了:

this.datepipe.transform(this.orderDateFilter.toLocaleDateString(),'yyyy-MM-dd')

效果非常好!

4dbbbstv

4dbbbstv3#

如果您使用.NET客户端(例如blazor)调用API,请将日期转换为UTC date.ToUniversalTime(),然后使用date.ToString("o")将其格式化为ISO 8601。这将包括带有Z(祖鲁语)的时区。
或者转换为sortablesafe字符串,然后附加Zulu
date.ToString("s") + Z

相关问题