2016-11-30 155 views
2

我想將對象轉換爲JSON字符串。 對象包含兩個屬性:名稱URLC#JSON.NET - 將數組轉換爲JSON字符串,包括url

WebsiteInfo _wbInfo = new WebsiteInfo(); 
    _wbInfo.WebsiteName = "MyWebsite"; 
    _wbInfo.URL = "http://example.com/"; 

    string _jsonString = JsonConvert.SerializeObject(_wbInfo); 

這是結果:

{"WebsiteName":"MyWebsite","URL":"http://example.com/"} 

到目前爲止好,但如果我這個JSON字符串發送到我的JSON服務,我收到錯誤消息「404 Not Found」。

這是我的完整網址:

http://localhost:63124/Test/{"WebsiteName":"MyWebsite","URL":"http://example.com/"} 

如果我執行這個(沒有斜槓 「/」),它會工作:

http://localhost:63124/Test/{"WebsiteName":"MyWebsite","URL":"http:example.com"} 

還與逃脫斜線它將無法工作:

http://localhost:63124/Test/{"WebsiteName":"MyWebsite","URL":"http:%2F%2F/example.com%2F"} 

這是我的JSON服務:

[OperationContract] 
[WebInvoke(Method = "GET", UriTemplate = "/Test/{WebsiteInfo}", ResponseFormat = WebMessageFormat.Json)] 
string Test(string WebsiteInfo); 
+1

爲什麼你不使用POST方法,而是GET?並且發送數據模型不是像uri的一部分,而是像內容那樣? –

回答

0

簡而言之,您無法以您在此嘗試的方式發送JSON。

您的JSON對象應該在請求正文中發送。