简体   繁体   English

oData 查询中如何处理特殊字符?

[英]How are special characters handled in an oData query?

How is the & symbol handled in the following query in oData?在 oData 中的以下查询中如何处理 & 符号?

/vendordataservice.svc/vDataMapper_SourceMapVendor?&$filter=startswith(ParentName,'AT&T')&$top=7&$skip=0

I'm using EF3.5 and SQL2008.我正在使用 EF3.5 和 SQL2008。 When I send that to my oData service I get no data back.当我将它发送到我的 oData 服务时,我没有收到任何数据。

Do not use the “JavaScript String replace() Method”.不要使用“JavaScript String replace() 方法”。 It will replace the first occurrence of the special characters.它将替换第一次出现的特殊字符。 If you have 2 occurance of the same special characters in the filtering parameter, it will fail.如果过滤参数中有 2 个相同的特殊字符出现,它将失败。 So use the regular expression to replace the characters.所以使用正则表达式来替换字符。

function replaceSpecialCharacters(attribute) {
  // replace the single quotes
     attribute = attribute.replace(/'/g, "''");

     attribute = attribute.replace(/%/g, "%25");
     attribute = attribute.replace(/\+/g, "%2B");
     attribute = attribute.replace(/\//g, "%2F");
     attribute = attribute.replace(/\?/g, "%3F");

     attribute = attribute.replace(/#/g, "%23");
     attribute = attribute.replace(/&/g, "%26");
     return attribute;
}

Also pay attention, since the replacements also contains % then % itself should be replaced at the beginning还要注意,由于替换也包含%那么%本身应该在开头替换

Here is a list of characters that should be encoded prior to sending to SQL server over HTTP:以下是通过 HTTP 发送到 SQL Server 之前应编码的字符列表:

http://msdn.microsoft.com/en-us/library/aa226544(SQL.80).aspx http://msdn.microsoft.com/en-us/library/aa226544(SQL.80).aspx

Yes, the '&' symbol is one of them.是的,“&”符号就是其中之一。

如果过滤器参数被视为一个单词,您可以在参数之前和之后附加单引号的 ASCII 值,如下所示%27AT&T%27

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

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