0

I want to pass dynamic Parameters by rest URL to C# method with dynamic Parameters. For example, I want to call a rest URL like this:

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

to a C# method of one dynamic parameter, the dynamic parameter takes "user" and its value "admin", "brand" and its value "dell" etc., and process them.

Can somebody help me please.

Soufiane Tahiri
  • 171
  • 1
  • 15
Ali.K
  • 45
  • 1
  • 7

2 Answers2

1

If you want to use Web API with dynamic parameter, you need use it as POST method.

URL: http://localhost:2000/custom

Data:

{
user:"admin"
password:"admin",
brand:"dell",
limit:20
}
Hien Nguyen
  • 24,551
  • 7
  • 52
  • 62
  • Yes, I used it as a post method, this request is rest webservice and the data must be in the URL – Ali.K Feb 19 '19 at 08:16
  • parameters can be passed in URL as well and read like a dictionary. https://stackoverflow.com/questions/22419823/how-to-access-all-querystring-parameters-as-a-dictionary – Andrei Dragotoniu Feb 19 '19 at 08:18
  • how do I get a dictionary from "http://url/?a=1&b=2&c=3" the a and b and c will send to the param of type dictionary I think this will solve my problem – Ali.K Feb 19 '19 at 10:22
0

You can use NameValueCollection returned by ParseQueryString :

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");
...

But passing the user:pass in GET and in plaintext is such a bad (very bad) idea :)

Soufiane Tahiri
  • 171
  • 1
  • 15