0

I using C# with Web API. I have this server method:

[System.Web.Http.HttpPost]
public HttpResponseMessage Test(CompareOutput model)
{
    return new HttpResponseMessage(HttpStatusCode.OK);
}

This is my data-model:

public class CompareOutput
    {
        public CompareOutput(int wc, int source_start, int source_end, int suspected_start, int suspected_end)
        {
            this.WordsCount = wc;
            this.SourceStartChar = source_start;
            this.SourceEndChar = source_end;
            this.SuspectedStartChar = suspected_start;
            this.SuspectedEndChar = suspected_end;
        }

        public CompareOutput()
        {

        }

        [JsonProperty("wc")]
        public int WordsCount { get; set; }

        [JsonProperty("SoS")]
        public int SourceStartChar { get; set; }

        [JsonProperty("SoE")]
        public int SourceEndChar { get; set; }

        [JsonProperty("SuS")]
        public int SuspectedStartChar { get; set; }

        [JsonProperty("SuE")]
        public int SuspectedEndChar { get; set; }

        public override string ToString()
        {
            return string.Format("{0} -> {1} | {2} -> {3}", this.SourceStartChar, this.SourceEndChar, this.SuspectedStartChar, this.SuspectedEndChar);
        }
    }

Now, I trying to call this method with this data (placed in BODY of the HTTP request):

 {
    "wc":11,
    "SoS":366,
    "SoE":429,
    "SuS":393,
    "SuE":456
 }

This call isn't working. All the int members getting defualt values ("0").

When I trying to send this HTTP-BODY, it's working good:

{
  "WordsCount":11,
  "SourceStartChar":366,
  "SourceEndChar":429,
  "SuspectedStartChar":393,
  "SuspectedEndChar":456
}

I can understand from that - alternative names (which defined in the model with JsonPropertry attribute) isn't working.

How can I solve it?

Thank you.

No1Lives4Ever
  • 6,430
  • 19
  • 77
  • 140
  • Could try marking up the class with [DataContract] at top then [DataMember(Name="wc")] etc on each property and see if that works? – RoguePlanetoid Nov 23 '16 at 10:36
  • Yes, I tried it. This was my first shot. Not making any difference. – No1Lives4Ever Nov 23 '16 at 10:40
  • There is a bit similar problem: http://stackoverflow.com/questions/26882986/overwrite-json-property-name-in-c-sharp – owczarek Nov 23 '16 at 10:55
  • Take a look a this [answer](http://stackoverflow.com/questions/24897579/newtonsoft-json-for-net-is-ignoring-jsonproperty-tags). If you are just inspecting the input parameter in your `Test` method during debug it is most likely using deserialization that ignores these attributes. – Damian Nov 23 '16 at 10:55
  • @owczarek I dont find anything common between two topics. – No1Lives4Ever Nov 23 '16 at 12:26
  • @Damian - with JSON.NET it's working fine. But the built in deserialization of .net not working with JSON.NET... – No1Lives4Ever Nov 23 '16 at 12:27

1 Answers1

0

Try to decorate your properties this way...

[JsonProperty(PropertyName = "wc")]
public int WordsCount { get; set; }

[JsonProperty(PropertyName = "SoS")]
public int SourceStartChar { get; set; }

[JsonProperty(PropertyName = "SoE")]
public int SourceEndChar { get; set; }

[JsonProperty(PropertyName = "SuS")]
public int SuspectedStartChar { get; set; }

[JsonProperty(PropertyName = "SuE")]
public int SuspectedEndChar { get; set; }