【问题标题】:ASP.NET Web API 2 send Dictionary<int, decimal> as GET URI request [duplicate]ASP.NET Web API 2 发送 Dictionary<int, decimal> 作为 GET URI 请求 [重复]
【发布时间】:2019-03-13 10:16:01
【问题描述】:

我在发送 Dictionary&lt;int, decimal&gt; 作为 GET URI 请求时遇到问题。我可以轻松地在 POST 请求正文中发送具有类似值的请求并使其工作:

"test": {
  "2019": 30.0916481889
}

https://stackoverflow.com/a/34030142/3850405

但是,由于我没有创建或修改任何内容,因此为了清楚起见,我不想使用正确的 HTTP 方法。正如您在上面看到的,JsonFormatter 没有从 Javascript Array 绑定 Dictionary,这就是需要使用对象的原因。

这是我正在使用的方法。我试图省略[FromUri],但该值始终为空。还尝试使用Dictionary&lt;int, int&gt;Dictionary&lt;string, string&gt; 来防止文化差异。

[Route("api/test/dictionary")]
[HttpGet]
public async Task<IHttpActionResult> TestDictionary([FromUri]Dictionary<int, decimal> test){

}

我尝试过的请求,所有组合都尝试过使用和不使用 URL 编码:

https://localhost:4431/api/test/dictionary?test=%7B%222019%22%3A%2230.0916481889%22%7D

https://localhost:4431/api/test/dictionary?test=%22test%22%3A%7B%222019%22%3A%2230.0916481889%22%7D

https://localhost:4431/api/test/dictionary?test="test":{"2019":"30"}

https://localhost:4431/api/test/dictionary?test={"2019":"30"}

https://localhost:4431/api/test/dictionary?test={"2019":30}

https://localhost:4431/api/test/dictionary?test={2019:30}

https://localhost:4431/api/test/dictionary?test=2019:30

https://localhost:4431/api/test/dictionary?test=2019

https://localhost:4431/api/test/dictionary?test=2019&test=30(从如何处理数组的角度来看https://stackoverflow.com/a/11100414/3850405

收到请求时是这样的:

如何将字典作为 GET URI 请求发送?即使 RFC2616 删除了 GET 请求的句子 the message-body SHOULD be ignored when handling the request,我的经验是它可能会在更大的系统中导致问题。缓存/代理无法按预期工作,列举一些很容易发生并且调试起来很痛苦的怪癖。

https://stackoverflow.com/a/983458/3850405

【问题讨论】:

  • @mjwills 我希望我们当前的基础架构能够缓存这些值,这不会发生在 POST 请求中。
  • 你试过了吗:localhost:4431/api/test/dictionary?test=[{"2019":"30"}]?
  • @mjwills 工作完美,现在标记为重复。我在发布之前尝试搜索,但我没有找到该线程。谢谢!

标签: javascript c# http dictionary asp.net-web-api2


【解决方案1】:

【讨论】:

    猜你喜欢
    • 2018-06-30
    • 1970-01-01
    • 2016-05-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多