-1

This is under controller

[HttpPost]
    public void SendMailMessageFromandToList(string fromusername, MailMessage mailMessage,List<string> tousernamelst)
    {
    }

I need to know how can I call this method using HttpClient. I have tried passing only class object and it works fine.

confClient.PostAsJsonAsync("api/Utility/SendMailMessageFromandToList?fromusername="+ fromusername +"", mail1); 
Sam S
  • 93
  • 2
  • 13
  • You mean you are using HttpClient, and want to know how to call the action and pass in the name, message and list? – Andy Wiesendanger Feb 17 '16 at 16:23
  • Yes, I want to know, how can i pass the three parameters at once in PostAsJsonAsync. – Sam S Feb 17 '16 at 16:37
  • What have you tried so far? Have you read any documentation on [PostAsJsonAsync](https://msdn.microsoft.com/en-us/library/hh944521(v=vs.118).aspx)? What about on structuring a [web api method and parameter binding](http://www.asp.net/web-api/overview/formats-and-model-binding/parameter-binding-in-aspnet-web-api)? You do have to put in some effort. – Igor Feb 17 '16 at 16:57
  • I tried reading through online documentation, I was not able to find any links online where both class object and list are being passed. I know how to pass multiple string parameters or multiple string parameters along with only list or class but not both at same time. – Sam S Feb 17 '16 at 17:04

1 Answers1

0

Generally you need to use standard http query syntax to add multiple parameters:

/api/Utility/SendMailMessageFromandToList**?**fromusername=example**&**mailMessage=...

For passing a list parameter in the url you can follow the following method stated in this post: Passing a List<string> to an MVC Web API method using the browser bar

The query string parameter should be of this format .../APIName?parameter[]=value1&parameter[]=value2&....

Update

You can pass a complex object in the PostAsJsonAsync method as second parameter:

class ExampleModel {
 public string property1 {get;set;}
 public IList<string> property2 {get;set;}
}
var complexObject = new ExampleModel();
//Set your values here then post it
client.PostAsJsonAsync("api/Utility/SendMailMessageFromandToList",complexObject);

You would only need to create an object with your three types as properties.

Community
  • 1
  • 1
edesr
  • 200
  • 1
  • 1
  • 12
  • Thank you for your reply. I will try this. – Sam S Feb 17 '16 at 17:14
  • This method would be good if I have less number of parameters in list, not if I have more than 10. Is there any other method, I can use to pass string, list and class object. – Sam S Feb 17 '16 at 17:48
  • I am sorry, I was busy with trying different methods with the code to pass values ,hence, was late to reply.As suggested by you, complexObject would be anything like list or Class, right?. If that is the case I have tried passing only class or only list to web api, and that works. Sorry, but my query is I need to pass both list and class at same time. – Sam S Feb 17 '16 at 19:27
  • You can define a model class with all the types you need to pass and use this model class for the postback. You will receive this model class on the server and can extract all the values you need. – edesr Feb 17 '16 at 19:37
  • Yeah, I think that should work. Thanks for the suggestion. – Sam S Feb 17 '16 at 19:45