5

I'm experiencing a simple yet annoying issue when using the PostAsJsonAsync<T>(..) extension method and I can't find information on fixing the following issue anywhere.

My issue is that the Json that gets generated uses PascaCasing and I require camelCasing as per the actual standard.

Here's a simple sample that can reproduce the issue (source: http://www.codeproject.com/Articles/611176/Calling-ASP-NET-WebAPI-using-HttpClient):

        HttpClient client = new HttpClient();
        client.BaseAddress = new Uri("http://localhost:56851/");

         client.DefaultRequestHeaders.Accept.Add(
            new MediaTypeWithQualityHeaderValue("application/json"));

        var user = new Users();

        user.FirstName = txtFirst.Text;
        user.Company = txtCompany.Text;
        user.LastName = txtLas.Text;
        user.Email = txtEmail.Text;
        user.PhoneNo = txtPhone.Text;
        user.Email = txtEmail.Text;

        var response = client.PostAsJsonAsync("api/User", user).Result;

        if (response.IsSuccessStatusCode)
        {
            MessageBox.Show("User Added");
            txtFirst.Text = "";
            txtLas.Text = "";
            txtPhone.Text = "";
            txtEmail.Text = "";
            txtCompany.Text = "";
            GetData();
        }
        else
        {
            MessageBox.Show("Error Code" + 
            response.StatusCode + " : Message - " + response.ReasonPhrase);
        }

1 Answer 1

3

try just sending an anonymous type.

var user = new
{
    firstName = txtFirst.Text,
    company = txtCompany.Text,
    lastName = txtLas.Text,
    email = txtEmail.Text,
    phoneNo = txtPhone.Text,
    email = txtEmail.Text
};

var response = await client.PostAsJsonAsync("api/User", user);
5
  • In my case I am sending an anonymous type, but this is definitely not the solution for me as I have complex data structures with more than one level and redefining my model at every request is not desired. Commented Nov 12, 2015 at 21:23
  • @LostBalloon gotcha.. maybe this will help newtonsoft.com/json/help/html/contractresolver.htm or james.newtonking.com/archive/2013/05/08/…
    – JamieD77
    Commented Nov 12, 2015 at 21:58
  • yeah, I ended up using Newtonsoft for that, but I can't believe they took the time to code PostAsJsonAsync<T>(..) only for it to be completely useless. Is there maybe something we're both missing? Commented Nov 13, 2015 at 14:45
  • Why do you say PostAsJsonAsync<T> is useless - because it uses Pascal Casing? This question states there is no standard: stackoverflow.com/questions/5543490/json-naming-convention - As with Views in MVC - I tend to us separate "ViewModels" for sending to external systems in any case as they rarely mirror our internal systems.
    – niico
    Commented Apr 25, 2017 at 15:08
  • How do I call PostAsJsonAsync from a ViewModel rather then a razor page? Commented May 24, 2021 at 21:17

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.