【发布时间】:2017-04-14 17:59:50
【问题描述】:
我想将一个对象转换为 JSON 字符串。 该对象包含两个属性:Name 和 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);
【问题讨论】:
-
为什么不使用 POST 方法而不是 GET 方法?并且发送数据模型不像 uri 的一部分,而是像内容一样?
标签: c# arrays json url json.net