【问题标题】:Passing parameters though url?通过url传递参数?
【发布时间】:2014-03-12 13:21:23
【问题描述】:

我正在尝试测试对我的一个函数的调用。

这是从我的网站外部调用的,其行为类似于 Web 服务。

测试我试图通过我的网址传递参数。

http://localhost:0000/APIService/UploadValuationDetails?ValuationDetails=[{property_details_address_address1{TagValue:'Test'},{ImageBase64:''}}]?Id=4785

我的服务代码:

public void UploadValuationDetails(Dictionary<string, ValuationDetails> JsonResult, int Id)
{
  DatabaseHelper DBH = new DatabaseHelper();

  foreach (var item in JsonResult)
  {                               //(ValuationId , TagName , TagValue , ImageBase64)
    DBH.WSValuationDetailUpdate(Id, item.Key, item.Value.TagValue, item.Value.ImageBase64); 
  }         
}

ValuationDetails 类:

public class ValuationDetails
{
   public string TagValue { get; set; }
   public string ImageBase64 { get; set; }
}

编辑改变了?对于 & 的第二个参数:


> http://localhost:0000/APIService/UploadValuationDetails?ValuationDetails={'property_details_address_address1':[{TagValue:'Test',ImageBase64:''}]}&Id=4785

将我的网址更改为高于断点的网址后,但值不正确。

编辑 2 试图在 json 结果中获取正确的值。


我想我更接近了:

http://localhost:0000/APIService/UploadValuationDetails?JsonResult={TagName:"property_details_address_address1",ValuationDetails:{TagValue:"Test","ImageBase64:""}}]&Id=4785

但现在我的 jsonResult = 0

【问题讨论】:

  • 当有多个参数时,必须用&amp;分隔。 ? 仅用于将路径名与第一个参数分开。
  • 我正在将 Url 直接写入浏览器,以便发送有关如何创建它的屏幕截图。
  • 这方面有什么进展吗?你的整流罩怎么样?
  • @paqogomez 没有,但发送给我数据的应用程序让它通过,幸运的是我能够继续前进。但是在将 json 直接放入 url 方面是行不通的。看着提琴手,它与我试图发送的格式相同,所以我不知道为什么它不起作用。可能是因为你不能设置 content type = application/json
  • @Pomster 你测试过你的json吗?因为它被顶起来了。

标签: c# json web-services url parameter-passing


【解决方案1】:

您应该使用与号 (&amp;) 分隔多个查询字符串参数。正如您所拥有的那样,您使用的是?,因此“?Id=4785”被解释为ValuationDetails 参数值的一部分。

更正:

                                       this is correct ┐
                                                       ↓
http://localhost:0000/APIService/UploadValuationDetails?ValuationDetails=
 [{property_details_address_address1{TagValue:'Test'},{ImageBase64:''}}]&Id=4785
                                                                        ↑
                                               but this should be fixed ┘

【讨论】:

  • @Cory 谢谢,已修复,但我的值仍然错误,请查看我的编辑。
  • @Pomster:你确定JSON 拥有.NET 对Dictionary&lt;string, ValuationDetails&gt; 的期望吗?我对内部工作原理还不够熟悉,不知道是否如此。另外,你第一次测试的URL中的参数值和你刚刚发布的不一样。
  • @Pomster 如果你直接输入值,你为什么告诉我你不是从url解析的?
  • @paqogomez ??我从 url 传递它,我直接在 url 中输入它,而我的本地主机正在运行并且函数上触发了一个断点。
【解决方案2】:

我认为最好也对 JSON 进行编码。 从那一刻起,您将拥有例如在您的数据中?或者 & 你也会得到一个例外。

【讨论】:

    【解决方案3】:

    您的 URL 字符串格式不正确。

    对于 URL 的分隔符和您将使用的参数?

    但要分隔参数使用&amp;

    http://localhost:0000/APIService/UploadValuationDetails?ValuationDetails=[{property_details_address_address1{TagValue:'Test'},{ImageBase64:''}}]&Id=4785

    您的 JSON 无效。

    我已经使用它了一点,但它仍然需要你的输入。

    [
        {
            "property_details_address_address1": {
                "TagValue": "Test"
            },
            "needs_a_name_here": {
                "ImageBase64": ""
            }
        }
    ]
    

    请注意,我已在名称周围加上引号。你的第二个对象也需要一个名字。

    我使用JSONLint 来验证和创建正确的 json

    【讨论】:

    • @paqogogomez 谢谢但是我的值仍然是错误的,请看我编辑的屏幕截图。
    • @Pomster 您的屏幕截图不显示任一参数的值。发布您用于解析 json 的代码。
    • 我正在将其写入我的网址
    • @Pomster 和触发断点时,JsonResult中没有值?
    • @Pomster 我刚刚将您的 json 粘贴到解析器中,出现 5 个错误。尝试将字符串[{property_details_address_address1{TagValue:'Test'},{ImageBase64:''}}] 放入this site
    猜你喜欢
    • 2014-05-22
    • 2023-04-08
    • 1970-01-01
    • 1970-01-01
    • 2022-01-22
    • 1970-01-01
    • 2017-10-12
    • 2012-03-12
    • 2018-09-06
    相关资源
    最近更新 更多