4

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);
        }
LostBalloon
  • 1,608
  • 3
  • 15
  • 31

1 Answers1

2

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);
abatishchev
  • 98,240
  • 88
  • 296
  • 433
JamieD77
  • 13,796
  • 1
  • 17
  • 27
  • 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. – LostBalloon Nov 12 '15 at 21:23
  • @LostBalloon gotcha.. maybe this will help http://www.newtonsoft.com/json/help/html/contractresolver.htm or http://james.newtonking.com/archive/2013/05/08/json-net-5-0-release-5-defaultsettings-and-extension-data – JamieD77 Nov 12 '15 at 21:58
  • yeah, I ended up using Newtonsoft for that, but I can't believe they took the time to code `PostAsJsonAsync(..)` only for it to be completely useless. Is there maybe something we're both missing? – LostBalloon Nov 13 '15 at 14:45
  • Why do you say PostAsJsonAsync is useless - because it uses Pascal Casing? This question states there is no standard: http://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 Apr 25 '17 at 15:08
  • How do I call PostAsJsonAsync from a ViewModel rather then a razor page? – Paul McCarthy May 24 '21 at 21:17