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

goo*_*ate 5 javascript sql entity-framework odata

如何在oData中的以下查询中处理&符号?

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

我正在使用EF3.5和SQL2008.当我将它发送到我的oData服务时,我得不到任何数据.

Rez*_*eza 7

不要使用"JavaScript String replace()方法".它将替换第一次出现的特殊字符.如果过滤参数中有2个相同的特殊字符,则会失败.因此,使用正则表达式替换字符.

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;
}
Run Code Online (Sandbox Code Playgroud)

另外要注意,因为置换也包含%那么%它自身应该在开始更换


goo*_*ate 6

以下是在通过 HTTP 发送到 SQL 服务器之前应编码的字符列表:

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

是的,“&”符号就是其中之一。