How to pass dictionary parameter to web-api method?

I have read all similar questions here, but I cannot come up with a solution. I am trying to call the web-api method:

[HttpGet] public SearchViewModel Get(SearchTypes type, string text, [FromUri]Dictionary<string, string> toyParams) { //Code here } 

and I want to get the last parameter from uri. I tried

http: // localhost: 39101 / #! / search / toys / fox? someParameter = 123

and

http: // localhost: 39101 / #! / search / toys / fox? toyParams [0] .Key = someParameter & toyParams [0] .Value = 123

but toyParams Dictionary always empty.

+5
source share
1 answer

One easy way, not a dictionary:

  //DTO public class SearchDTO { public int MyProperty1 { get; set; } public int MyProperty2 { get; set; } public int MyProperty3 { get; set; } } 

Where MyProperty1 , MyProperty2 , MyProperty3 are the parameters on the basis of which something needs to be searched.

  //GET Method public string Get([FromUri]SearchDTO searchDTO) { return "search result"; } 

The following is the url:

http: // localhost: 56880 / api / values? myproperty1 = 1 & myproperty2 = 2 & myproperty3 = 3

-4
source

Source: https://habr.com/ru/post/1233538/


All Articles