Home > Enterprise >  Posting to an ASP.NET Core controller with parameters
Posting to an ASP.NET Core controller with parameters

Time:04-26

I am trying to post to a .net controller using HttpClient, but the controller is never being hit....

Here is how I am trying to post:

NameValueCollection data = new NameValueCollection();
data.Add("someField", "someValue");

using (HttpClient client = new HttpClient())
{
    HttpResponseMessage response = await client.PostAsync(url,
        new FormUrlEncodedContent(data.Cast<string>()
            .SelectMany(key => data.GetValues(key), (key, value) => new { key, value })
            .ToDictionary(p => p.key, p => p.value))
    );
}

And then my controller:

[HttpPost("MyController")]
public void MyControllerMethod(string someField)
{
    // never being hit....
}

I can change it slightly and the method gets hit.... But in the following scenario, someField is null and it shouldn't be:

NameValueCollection data = new NameValueCollection();
data.Add("someField", "someValue");

var dictionary = data.Cast<string>()
    .SelectMany(key => data.GetValues(key), (key, value) => new { key, value })
    .ToDictionary(p => p.key, p => p.value);

var json = JsonConvert.SerializeObject(dictionary);
StringContent stringContect = new StringContent(json);

using (HttpClient client = new HttpClient())
{
    HttpResponseMessage response = await client.PostAsync(url, stringContect);
}

But like I said, in that scenario, the param someField isn't mapping the way I expect it to and is showing as NULL instead of "someValue"....

Any ideas?

Thanks!

CodePudding user response:

You should make model class, which will include all keys from form, like that:

public class FormModel // Or any name
{
    public string SomeField { get; set; }

    // Any fields
}

And then bind data from request to argument of controller method with FromFormAttribute, like that:

[HttpPost("MyController")]
public void MyControllerMethod([FromForm] FormModel model)
{
    var someField = model.SomeField;

    // Your logic
}

Read more about model binding here.

CodePudding user response:

Two ways to post single string parameter by using HttpClient below:

1.From Body

var json = JsonConvert.SerializeObject("someValue");
StringContent stringContect = new StringContent(json, Encoding.UTF8, "application/json");

using (HttpClient client = new HttpClient())
{
    HttpResponseMessage response = await client.PostAsync("https://localhost:portNum/xxx", 
                                                    stringContect);
}

Controller:

[HttpPost]
public async Task<ActionResult> Post([FromBody]string someField)
{
    //...
}

2.From Form

var formContent = new FormUrlEncodedContent(new[]
{
    new KeyValuePair<string, string>("someField", "someValue"),
});

using (HttpClient client = new HttpClient())
{
    HttpResponseMessage response = await client.PostAsync("https://localhost:portNum/xxx", 
                                                      formContent);
}

Controller:

[HttpPost]
public async Task<ActionResult> Index([FromForm]string someField)
{
    //...           
}
  • Related