代码之家  ›  专栏  ›  技术社区  ›  makerofthings7

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

  •  4
  • makerofthings7  · 技术社区  · 14 年前

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

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

    我使用的是EF3.5和SQL2008。当我发送到我的oData服务时,我没有得到任何数据。

    2 回复  |  直到 14 年前
        1
  •  7
  •   Reza    7 年前

    不要使用JavaScript String replace()方法。它将替换第一个出现的特殊字符。如果筛选参数中出现两个相同的特殊字符,则将失败。所以使用正则表达式替换字符。

    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;
    }
    

    还要注意,因为替换的也包含 % 然后 % 一开始就应该更换

        2
  •  4
  •   Rami A.    8 年前

    下面是在通过HTTP发送到SQL server之前应编码的字符列表:

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

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

        3
  •  0
  •   elemes    5 年前

    如果filter参数被看作一个单词,您可以在参数前后附加单引号的ASCII值,如下所示 %27AT&T%27