With a web api, how would you create a method that accepts a list of identifiers?

I want to have a method (api action method) that looks like this:

public List<User> GetUserList(int userId, string commaSeperatedListOfIds) { ... } 

From what I understand, the web api controller will automatically convert the json request and map the parameters, so it is best to create your own class as a parameter, for example:

 public class UserListRequest { public int UserId {get;set;} public string CsvListOfIds {get;set;} } 
+4
source share
2 answers

From what I understand, the web api controller will automatically convert the json request and display the parameters, so it's best to create your own class

Yes. If the data sent with the request can be mapped to an object, the Web API will convert the JSON or XML object to a class object.

If you want your json data to fall inside the object in your method, first determine the class in which your json data will be stored.

 public class UserListRequest { public int UserId { get; set; } public string CsvListOfIds { get; set; } } 

Later change your method signature to get an object of this class:

 [HttpPost] public List<User> GetUserList(UserListRequest obj) { List<User> list = new List<User>(); list.Add(new User { ID = obj.UserId }); list.Add(new User { ID = obj.UserId + 1 }); //something return list; } 

Remember to use the [HttpPost] attribute. There should also be one post method that you can comment on the already provided Post method in the controller. (If you want your controller to have multiple send methods, see this question )

You can send a request through jquery or Fiddler (or in any other way). I tested it with Fiddler. You can see Web API Debugging via Fiddler

Once your controller is configured, then create a project and run it in debug mode. Start Fiddler and go to the composer.

Paste your controller URI (from IE / Brower) into the address bar and select Post as the Http method. Your URI will look like this:

 http://localhost:60291/api/values/GetUserList 

In Fiddler Composer -> Title Header, specify

 Content-Type: application/json 

In Fiddler Composer -> Request Body, specify your JSON

 { "UserId": 1, "CsvListOfIds": "2,3,4,5" } 

(You can also create your C # template class from json using http://json2csharp.com/ )

Put the debug point in your method in the controller, and then in Fiddler Hit Execute , you will see that your debug point in Visual studio is Hit, and the data is populated into the parameter. enter image description here

You can later return a List<User> and you will see the answer in Fiddler.

 [{"ID":1},{"ID":2}] 

I temporarily created a User class, for example:

 public class User { public int ID { get; set; } } 

Hope this helps.

+1
source

Yes, to a large extent. Create a json object with the same property names as the ViewModel (UserListRequest) in javascript before passing it through the POST request to the controller. Use IEnumerable <> when you have lists to go to the controller. In your example, this could be an IEnumerable<int> (for type safety) in your UserListRequest class for a list of identifiers. On the javascript side, it could be an array.

EDIT: GET

 $.get(url, function (data) { var ids= (data.CsvListOfIds.split(',')); }); 
0
source

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


All Articles