-6

In my API model binder get request I want to convert a comma separated string that is passed through the get request to a list. I want to only do it for once of the properties in my model so IModelBinder would not be the best to use, maybe a TypeConverter?

How would I go about doing this?

Some of you seem confused regarding m vague question. I want to add mapping within the http middleware not after the api call has been made. There are numerous approaches such as the itypeconverter, action filter. This means i will have to call the method over and over.

So if I have a API call called

public IActionResult getUserConfigs(List<string>UserIds, list<int> permmisionIds)

They send in a request e.g. UserIds=1,2,3,4,5, how do I plug in a middleware component to map the string to a list.

This needs to work in a dot net core project.

James Andrew Smith
  • 1,516
  • 2
  • 23
  • 43
  • https://stackoverflow.com/questions/9981330/how-to-pass-an-array-of-integers-to-asp-net-web-api – CodeCaster Jul 17 '17 at 12:45
  • 3
    Possible duplicate of [How can I convert comma separated string into a List](https://stackoverflow.com/questions/9301268/how-can-i-convert-comma-separated-string-into-a-listint) – Maddy Jul 17 '17 at 14:01
  • As I said in my question that I want to set up in a model binder approach and re-maps the input fields send over a http request e.g. userIds=1,2,3 to map it to a list. – James Andrew Smith Jul 17 '17 at 14:56
  • Why do you send a JSON property `UserIds: '1,2,3,4,5'` instead of `UserIds: [1, 2, 3, 4, 5]`? The latter will automatically bind to the `IList`. – krillgar Jul 17 '17 at 15:24

1 Answers1

0

Since you've supplied no details about what your model looks like, or any code at all, I'm going to construct a simple example to answer the question as I understand it...

This will receive a string value, separate it into an array and build an instance of a model from the separated values:

public class User
{
    public string Username { get; set; }
    public string Password { get; set; }
}

public void ReceiveUserString(string source)
{
    string[] UserString = source.Split(',');

    var User = new User();
    foreach (string u in UserString)
    {
        User.Username = u[0];
        User.Password = u[1];
    }
}

The User object can then be saved to your database or... whatever it is that you want to do with it on your API.

If you're getting a list of users, you'll need a second delimiter to split lines on:

string[] UsersFromString = source.Split('|');

foreach (string user in UsersFromString)
{
    var User = new User();
    string[] Fields = user .Split(',');

    User.Username = Fields[0];
    User.Password = Fields[1];
}

From your question:

I want to convert a comma separated string that is passed through the get request to a list.

You can change the code slightly to have your output as a List<User>:

string[] UsersFromString = source.Split('|');

List<User> Users = new List<User>();
foreach (string user in UsersFromString)
{
    string[] Fields = user .Split(',');
    Users.Add(new User {User.Username = Fields[0], User.Password = Fields[1] });
}
Ortund
  • 8,095
  • 18
  • 71
  • 139
  • That seems like a perfectly valid answer to a mostly vague question. Downvoters, please provide an argument as to which this answer could be improved. – Eric Wu Jul 17 '17 at 13:55