简体   繁体   English

当Uri逃离&符号时,OData筛选器选项将失败

[英]OData filter options fail when Uri has escaped ampersand

I'm currently using OData v3, but this seems to fail (just differently) in OData v4 as well. 我当前使用的是OData v3,但这在OData v4中似乎也失败了(只是有所不同)。

Let's say I have the following Uri: 假设我有以下Uri:
http://myurl.com/odata/SomeEndpoint ?$filter=FieldId eq 1&$top=10 http://myurl.com/odata/SomeEndpoint?$ filter = FieldId eq 1&$ ​​top = 10

Perfect, type that into Postman or a browser, works great. 完美,将其输入Postman或浏览器,效果很好。 But If I encode the Uri it fails. 但是,如果我对Uri进行编码,它将失败。

HTML Encoding: HTML编码:
http://myurl.com/odata/SomeEndpoint ?$filter=FieldId eq 1&$top=10 http://myurl.com/odata/SomeEndpoint?$ filter = FieldId eq 1& $ top = 10

Everything before the escaped character will get applied to the query (so $filter still works), but everything after the escaped character is ignored ($top is not applied). 转义字符之前的所有内容都将应用于查询(因此$ filter仍然有效),但转义字符之后的所有内容都将被忽略(未应用$ top)。

URI Encoding (I tried both): URI编码(我都尝试过):
http://myurl.com/odata/SomeEndpoint ?$filter=FieldId eq 1%26$top=10 http://myurl.com/odata/SomeEndpoint ?$filter=FieldId eq 1%26%$top=10 http://myurl.com/odata/SomeEndpoint?$ filter = FieldId eq 1%26 $ top = 10 http://myurl.com/odata/SomeEndpoint?$ filter = FieldId eq 1%26%$ top = 10

Both cause OData to throw an exception when trying to apply the query options with the following error (paraphrased since this is from memory): 两者都导致OData在尝试应用查询选项时引发异常,并出现以下错误(解释为由于来自内存):

'&' is in invalid character in query string.
$filter=FieldId eq 1&$top=10

As you can see, this is undesired and an issue. 如您所见,这是不希望的,也是一个问题。 If my client's encode their Uri before sending a request it won't work. 如果我的客户在发送请求之前对Uri进行了编码,那么它将无法正常工作。 Also, the links generated by OData in the result are encoded, for instance, the next page link: 同样,由OData在结果中生成的链接也会被编码,例如,下一页链接:

<link rel="next" href="http://myurl.com/odata/SomeEndpoint?$filter=FieldId%20eq%201&amp;$skip=1" /> <link rel =“ next” href =“ http://myurl.com/odata/SomeEndpoint?$filter=FieldId%20eq%201&amp;$skip=1” />

OData doesn't seem to mind the %20 (space) that works fine, but $skip is actually ignored because of the escaped ampersand (&amp;) if you just change it to a real ampersand (&) it works fine. OData似乎并不介意可以正常工作的%20(空格),但实际上$ skip会被忽略,因为如果将其改成真实的“&”号,它会正常工作,因为转义了&符号。

Is there a way to fix this? 有没有办法解决这个问题? It seems to be an issue with OData. OData似乎是一个问题。

For reference here is how I'm mapping my routes: 供参考,这是我如何映射路线:

#pragma warning disable CS0618 // OData v3 route mapping.
config.Routes.MapODataRoute(
    routeName: "odata",
    routePrefix: "odata",
    model: model);
#pragma warning restore CS0618

I'm not doing anything custom. 我没有做任何自定义。

UPDATE: 更新:
When I set my ACCEPT header to XML I get: 当我将ACCEPT标头设置为XML时,我得到:
<link rel="next" href="http://myurl.com/odata/SomeEndpoint?$filter=FieldId%20eq%201&amp;$skip=1" /> <link rel =“ next” href =“ http://myurl.com/odata/SomeEndpoint?$filter=FieldId%20eq%201&amp;$skip=1” />

When I set my ACCEPT header to JSON I get: 当我将ACCEPT标头设置为JSON时,我得到:
odata.nextLink": " http://webapi.mydlweb.com/api/test/odata/v3/Checks ?$filter=StoreId%20eq%2040&pagesize=1&$skip=1" odata.nextLink“:“ http://webapi.mydlweb.com/api/test/odata/v3/Checks?$ filter = StoreId%20eq%2040&pagesize = 1&$ ​​skip = 1”

Per the comment below, the JSON link is generated correctly. 根据以下评论,JSON链接已正确生成。 I can click it and it works because the ampersand is not encoded. 我可以单击它,因为与号未编码,所以它可以工作。 However the XML link DOES encode the ampersand and is thus causing it to not work. 但是,XML链接确实对“&”号进行编码,因此导致其无法正常工作。 Is that normal when encoding XML then? 那么编码XML是正常的吗? How then does the client know which ampersands to "unencode" and which to leave encoded? 然后,客户如何知道要对哪些“&”号进行“取消编码”以及对哪些符号进行编码? For instance, if you use an ampersand inside of a filter (not to separate query strings) then it DOES need to be encoded. 例如,如果在过滤器内部使用“&”号(而不是分隔查询字符串),则需要对它进行编码。

UPDATE 2: I guess it is XML encoding, and that's just the way it is. 更新2:我想这是XML编码,就是这样。

在此处输入图片说明

You can't escape the ampersand in a querystring and expect it to behave like an unescaped ampersand in a querystring. 您不能逃避查询字符串中的“&”号,并期望它的行为像查询字符串中未转义的“&”号一样。 This doesn't have anything to do with odata, just normal querystring behavior. 这与odata无关,只是正常的查询字符串行为。 By escaping it, the parser will not interpret it as a delimiter of key/value pairs but as a literal ampersand in the value. 通过转义,解析器不会将其解释为键/值对的定界符,而是解释为值中的文字“&”号。

EXAMPLE: This google search works: 示例:此Google搜索有效:

https://www.google.com/search?safe=off&q=test

But, this one does not: 但是,这一点没有:

https://www.google.com/search?safe=off%26q=test

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM