【问题标题】:C# JSON.NET - Convert Array to JSON string thats including urlC# JSON.NET - 将数组转换为包含 url 的 JSON 字符串
【发布时间】:2017-04-14 17:59:50
【问题描述】:

我想将一个对象转换为 JSON 字符串。 该对象包含两个属性:NameURL

  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


【解决方案1】:

简而言之,您不能以您在此处尝试的方式发送 JSON。

您的 JSON 对象应改为在请求正文中发送。

【讨论】:

    猜你喜欢
    • 2015-07-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-10-16
    相关资源
    最近更新 更多