2012-10-27 101 views
1

我有一組允許JSON格式進行消息交換的WCF服務。JSON服務傳遞空/空字符串參數值

[WebInvoke(Method = "GET", ResponseFormat = WebMessageFormat.Json, RequestFormat = WebMessageFormat.Json, UriTemplate = "SearchStores/{accountId}/{storeName}")] 
public IList<Store> SearchStores(string accountId, string storeName) 

如何將空/空的storeName傳遞給方法?如果我使用下面的url來調用該方法,我得到404未找到錯誤。

服務器名稱:端口/爲MyService/SearchStores/1/

請建議表示感謝。

+0

字母「空」,不帶引號,表示在JSON空值。當然,無論你在說什麼都能接受是另一回事。 –

回答

0

您可以修改您的模板以改爲使用查詢字符串參數。如果不能在查詢中指定

UriTemplate = "SearchStores?accountId={accountId}&storeName={storeName}" 

這樣,accountIdstoreName將是空的。如果只允許storeName爲空,那麼您當然可以按原樣保留accountId,並使用storeName的查詢字符串參數構建UriTemplate

編輯

如果您不允許使用的查詢字符串,那麼您可以在您的UriTemplate使用默認爲空值作爲MSDN UriTemplate documentation描述。在你的情況下:

UriTemplate = "SearchStores/{accountId}/{storeName=null} 

請注意,一旦你使用默認的空值,右邊的所有段也必須有空默認值。舉例來說,這將是有效的:

UriTemplate = "SearchStores/{accountId}/{storeName=null}/{someParam=null} 

但這並不:

UriTemplate = "SearchStores/{accountId}/{storeName=null}/{someParam} 
+0

我無法使用查詢字符串表示法。它必須是路由數據。有沒有一種方法可以使用路徑表示法爲storeName指定默認(空/空字符串)?謝謝 – joblot

+0

@joblot - 根據你的約束更新了我的答案。 –

+0

謝謝你的幫助。 – joblot

相關問題