如何转义 OData 查询中使用的单引号?

发布于 2024-09-28 07:30:00 字数 382 浏览 4 评论 0原文

我正在使用 OData 查询我的数据库。当“adapterName”仅包含文本时,以下代码行可以正常工作。

ds.query('/DataAdapters?$filter=Name eq \'' + adapterName + '\'', ifmgr_CreateAdapter_Step1, onGenericFailure, '');

如果“adapterName”包含单引号,则会失败。我尝试使用以下代码转义单引号:

adapterName = adapterName.replace(/\'/g, '\\\'');

尽管这正确地转义了用户定义的文本,但该函数仍然失败。谁能告诉我查询中文本的正确格式是什么?

I am using OData to query my database. The following line of code works fine when “adapterName” just contains text.

ds.query('/DataAdapters?$filter=Name eq \'' + adapterName + '\'', ifmgr_CreateAdapter_Step1, onGenericFailure, '');

If “adapterName” contains a single quote it fails. I tried escaping the single quote by using the following code:

adapterName = adapterName.replace(/\'/g, '\\\'');

Although this correctly escapes the user defined text the function still fails. Can anyone tell me what the correct format is for text in the query?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

暖阳 2024-10-05 07:30:00

实际上 %27 不是一个解决方案。正确的转义方法是在字符串中放置两个单引号,而不是一个。
在示例中“o''clock”

Actually %27 is not a solution. The correct way to escape is to place two single quotes into the string instead one.
In example "o''clock"

世态炎凉 2024-10-05 07:30:00

我想稍微扩展一下答案,以便它也适用于调用 oData 服务操作操作。发布的答案是正确的,但是服务操作的参数必须按照特定的顺序进行编码。

oData 服务操作接收原始类型参数,其中字符串包含在 ' 中,这样有效的 url(预编码)将是这样的

AddString?value='o''clock'

这将导致服务器看到

AddString?value='o '

'clock'

将产生“错误请求 - 查询语法错误”。

要纠正此问题,您必须在插入 url 之前对 ' 进行双重转义并对其进行 UrlEncode。

不要对 url 本身进行 UrlEncode。

这是一个可行的示例。

// value passed as "o'clock"
public async Task AddString(string value)
{
    // Escape ' with '' and UrlEncode value
    value = HttpUtility.UrlEncode(value.Replace("'", "''"));

    string url = String.Format("AddString?value='{0}'", value);

    // No need to UrlEncode url here as dynamic content has already been escaped 

    // Execute .....
}

[WebGet]
public void AddString(string value) 
{
    // here value will be "o'clock"
}

I want to expand upon the answer a bit so that it also applies to calling an oData Service Operation Action. The answer posted answer is correct, but there is a specific order in which the parameters to a service operation must encoded.

oData Service Operations receive primitive type parameters where strings are enclosed in a ' such that a valid url (pre encoding) will be as such

AddString?value='o''clock'

This will cause the server to see

AddString?value='o'

and

'clock'

will produce "Bad Request - Error in query syntax."

To correct this, you must double escape the ' and UrlEncode it prior to insertion into the url.

Do not UrlEncode the url itself.

Here's an example that will work.

// value passed as "o'clock"
public async Task AddString(string value)
{
    // Escape ' with '' and UrlEncode value
    value = HttpUtility.UrlEncode(value.Replace("'", "''"));

    string url = String.Format("AddString?value='{0}'", value);

    // No need to UrlEncode url here as dynamic content has already been escaped 

    // Execute .....
}

[WebGet]
public void AddString(string value) 
{
    // here value will be "o'clock"
}
西瑶 2024-10-05 07:30:00

它实际上在 oData 文档中进行了描述: http://docs.oasis-open.org/odata/odata/v4.01/cs01/part2-url-conventions/odata-v4.01-cs01- part2-url-conventions.html#sec_URLComponents

例如,这些规则之一是字符串文字中的单引号表示为两个连续的单引号。

示例 3:有效的 OData URL:

http://host/service/People('O''Neil')

http://host/service/People(%27O%27%27Neil) %27)

http://host/service/People%28%27O% 27%27尼尔%27%29

http://host/service/Categories('Sm​​artphone%2FTablet')

示例 4:无效的 OData URL:

http://host/service/People('O'Neil')

http://host/service/People('O%27Neil')

http://host/service/Categories('智能手机/平板电脑')

第一个和第二个示例无效,因为字符串中的单引号 >文字必须表示为两个连续的单引号。第三个示例无效,因为正斜杠被解释为路径段分隔符和类别(“智能手机不是有效的 OData 路径段,平板电脑也不是”)。

It's actually described in oData docs: http://docs.oasis-open.org/odata/odata/v4.01/cs01/part2-url-conventions/odata-v4.01-cs01-part2-url-conventions.html#sec_URLComponents

For example, one of these rules is that single quotes within string literals are represented as two consecutive single quotes.

Example 3: valid OData URLs:

http://host/service/People('O''Neil')

http://host/service/People(%27O%27%27Neil%27)

http://host/service/People%28%27O%27%27Neil%27%29

http://host/service/Categories('Smartphone%2FTablet')

Example 4: invalid OData URLs:

http://host/service/People('O'Neil')

http://host/service/People('O%27Neil')

http://host/service/Categories('Smartphone/Tablet')

The first and second examples are invalid because a single quote in a string > literal must be represented as two consecutive single quotes. The third example is invalid because forward slashes are interpreted as path segment separators and Categories('Smartphone is not a valid OData path segment, nor is Tablet').

柠北森屋 2024-10-05 07:30:00

当使用 wit substringof 时,需要使用 4 个而不是 1 个撇号来转义:


a'b->
$filter=(substringof('a'''b', FirstName))

When using wit substringof it needs to be escaped by having 4 instead of 1 apostrophe:


a'b ->
$filter=(substringof('a''''b', FirstName))

伊面 2024-10-05 07:30:00

我没有使用 $filter=Title eq 'text',而是

使用 oDatastartswith() 函数。

$filter=startswith(Title, key)

然后我传入尽可能多的 key 。

var pos = key.indexOf("'");
if(pos > -1) {
key = key.substring(0, pos);
<代码>}

Instead of using $filter=Title eq 'text'

I am using the oData startswith() function.

$filter=startswith(Title, key)

and then I pass in as much of key as I can.

var pos = key.indexOf("'");
if(pos > -1) {
key = key.substring(0, pos);
}

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文