2010-11-19 88 views
2

在oData的以下查詢中如何處理&符號?如何在oData查詢中處理特殊字符?

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

我正在使用EF3.5和SQL2008。當我將它發送到我的oData服務時,我沒有收到任何數據。

+0

你生產這個自己?你不應該編碼嗎? – 2010-11-19 21:57:38

+0

@Craig - 當應用過濾器時,GET字符串由我的Telerik控件創建。他們應該編碼嗎?我不確定,但也許。還有哪些字符應該被編碼?它應該是HTML編碼? – LamonteCristo 2010-11-20 03:41:50

+0

按照RFC,'&'字符在URI中保留。是的,它應該被編碼。 – 2010-11-20 03:49:00

回答

3

請勿使用「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; 
} 

還要注意,由於置換也包含%然後%它自應在開始更換