Diego
Diego

Reputation: 2372

MVC Api Controller Serielized parameters

I am doing an MVC 5 Application, and I am calling a API controller method that is in another Solution.

I am using HttpClient(). and I am calling PostAsJsonAsync with some parameters, an instance of a class.

It looks like this.

string apiUrl = "localhost:8080/api/";
ContactWF contactWF = new contactWF();
contactWF.contact_id=0;
contactWF.UserOrigin_id=20006
contactWF.ProcessState_id=2;

using (HttpClient client = new HttpClient())
{
    client.BaseAddress = new Uri(apiUrl);
    client.DefaultRequestHeaders.Accept.Clear();
    client.DefaultRequestHeaders.Accept.Add(new System.Net.Http.Headers.MediaTypeWithQualityHeaderValue("application/json"));
    HttpResponseMessage response = await client.PostAsJsonAsync(apiUrl + "Contact/Method", contactWF);
    if (response.IsSuccessStatusCode)
    {
        return response.Content.ReadAsAsync<int>().Result;
    }
}

My API controller method is like this.

[ActionName("Method")]
[HttpGet]
public int Method([FromBody] ContactWF userwf)
{
    return 10;
}

It Works fine...

My problem is when I try Serialized the parameter class instance I replace line

HttpResponseMessage response = await client.PostAsJsonAsync(apiUrl + "Contact/Method", contactWF);

with this one

string jsonData = JsonConvert.SerializeObject(contactWF);
HttpResponseMessage response = client.PostAsJsonAsync("api/Contact/Method", jsonData).Result;

I've got an Error:405...

It looks like the Json string it is not recognize as a Parameter.

My Json string looks like this.

"{\"Contact_id\":0,\"Description\":null,\"ProcessState_id\":2,\"Type_id\":0,\"Object_id\":0,\"Parent_id\":null}"

that is ContactWD class converter to json.

What´s wrong?

Upvotes: 0

Views: 889

Answers (2)

Hung Quach
Hung Quach

Reputation: 2177

Change verb to HttpPost in your api controller

[ActionName("Method")]
[HttpPost]
public int Method([FromBody] ContactWF userwf)
{
    return 10;
}

Update

You don't need to serialize object in PostAsJsonAsync

HttpResponseMessage response = client.PostAsJsonAsync("api/Contact/Method", contactWF).Result;

Take a look at sample code from microsoft https://learn.microsoft.com/en-us/aspnet/core/mvc/controllers/testing

internal class NewIdeaDto
    {
        public NewIdeaDto(string name, string description, int sessionId)
        {
            Name = name;
            Description = description;
            SessionId = sessionId;
        }

        public string Name { get; set; }
        public string Description { get; set; }
        public int SessionId { get; set; }
    }

//Arrange
var newIdea = new NewIdeaDto("Name", "", 1);

// Act
var response = await _client.PostAsJsonAsync("/api/ideas/create", newIdea);

// Assert
Assert.Equal(HttpStatusCode.BadRequest, response.StatusCode);

Upvotes: 1

Vitaliy Smolyakov
Vitaliy Smolyakov

Reputation: 472

Method PostAsJsonAsync serialize parameter object himself, so it serialized your json string again.

If you need serialize object himself for some reason, then use method HttpClient.PostAsync

string jsonData = JsonConvert.SerializeObject(contactWF);
var stringContent = new StringContent(jsonData, Encoding.UTF8, "application/json");
HttpResponseMessage response = await client.PostAsync("api/Filler/CountMensajeByUser", stringContent);

Upvotes: 1

Related Questions