![](/img/trans.png)
[英]Odata V4 how to allow special character like lastname with single quote O'Keefe
[英]how to allow ' character in OData endpoint URL
如果實體存在,我可以使用OData端點為客戶端的名字和姓氏進行過濾。 當用戶在名稱字符串中使用'字符時會發生問題,這將破壞OData URI解析器,我的測試字符串將是
http://localhost.com/Clients?$ filter =名字eq'test'20001'和姓氏eq'test20001'
我的堆棧跟蹤
{
"error": {
"code": "",
"message": "The query specified in the URI is not valid. There is an unterminated string literal at position 23 in 'FirstName eq 'test20001'.",
"innererror": {
"message": "There is an unterminated string literal at position 23 in 'FirstName eq 'test20001'.",
"type": "Microsoft.OData.ODataException",
"stacktrace": " at Microsoft.OData.UriParser.ExpressionLexer.NextToken()\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 System.Web.OData.Query.FilterQueryOption.get_FilterClause()\r\n at System.Web.OData.Query.Validators.FilterQueryValidator.Validate(FilterQueryOption filterQueryOption, ODataValidationSettings settings)\r\n at System.Web.OData.Query.FilterQueryOption.Validate(ODataValidationSettings validationSettings)\r\n at System.Web.OData.Query.Validators.ODataQueryValidator.Validate(ODataQueryOptions options, ODataValidationSettings validationSettings)\r\n at System.Web.OData.Query.ODataQueryOptions.Validate(ODataValidationSettings validationSettings)\r\n at System.Web.OData.EnableQueryAttribute.ValidateQuery(HttpRequestMessage request, ODataQueryOptions queryOptions)\r\n at System.Web.OData.EnableQueryAttribute.ExecuteQuery(Object response, HttpRequestMessage request, HttpActionDescriptor actionDescriptor, ODataQueryContext queryContext)\r\n at System.Web.OData.EnableQueryAttribute.OnActionExecuted(HttpActionExecutedContext actionExecutedContext)"
}
}
}
這是我如何解決
var filter = '$filter=FirstName eq \'' + escapeString(self.selectedClient().FirstName())
function escapeString(string) {
string = string.replace(/'/g, '\'\'');
string = string.replace(/&/g, '%26');
return string;
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.