【问题标题】:Pass dynamic Parameters by rest URL to C# method with dynamic Parameters [duplicate]通过rest URL将动态参数传递给具有动态参数的C#方法[重复]
【发布时间】:2019-02-19 07:57:05
【问题描述】:

我想通过 rest URL 将动态参数传递给具有动态参数的 C# 方法。 例如,我想这样调用一个rest URL:

http://localhost:2000/custom?**user=admin?password=admin?brand=dell&limit=20&price=20000&type1=laptop&type2=phone**

对于一个动态参数的C#方法,动态参数取“user”及其值“admin”、“brand”及其值“dell”等,并对其进行处理。

谁能帮帮我。

【问题讨论】:

  • 永远不要在 URL 中发送密码。如果您仅使用 https,请将其放在帖子中或放在标题中。

标签: c# asp.net post asp.net-web-api keyvaluepair


【解决方案1】:

如果你想使用带有动态参数的Web API,你需要使用它作为POST方法。

网址:http://localhost:2000/custom

数据:

{
user:"admin"
password:"admin",
brand:"dell",
limit:20
}

【讨论】:

  • 是的,我用的是post方式,这个请求是rest webservice,数据必须在url中
  • 参数也可以在 URL 中传递并像字典一样读取。 stackoverflow.com/questions/22419823/…
  • 如何从“url/?a=1&b=2&c=3”获取字典,a 和 b 和 c 将发送到类型字典的参数我认为这将解决我的问题
【解决方案2】:

你可以使用ParseQueryString返回的NameValueCollection

Uri myUri = new Uri("http://localhost:2000/custom?user=admin?password=admin?brand=dell&limit=20&price=20000&type1=laptop&type2=phone");
string user= HttpUtility.ParseQueryString(myUri.Query).Get("user");
string password= HttpUtility.ParseQueryString(myUri.Query).Get("password");
string brand= HttpUtility.ParseQueryString(myUri.Query).Get("brand");
string limit= HttpUtility.ParseQueryString(myUri.Query).Get("limit");
...

但是传递用户:以 GET 和明文形式传递是一个糟糕的(非常糟糕)的想法 :)

【讨论】:

    猜你喜欢
    • 2021-06-05
    • 1970-01-01
    • 2012-11-23
    • 2015-08-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多