Troubleshooting MVC Serialization

September 19, 2018    Asp.Net MVC Troubleshooting

Troubleshooting MVC Serialization

I was trying to POST from JavaScript to my Asp.Net MVC “Classic” controller and the parameter was null. I tried several different casings using $.ajax (really I’d rather use EcmaScript Proimses, but this project already has jQuery so I can’t rock the boat too much), but it kept serializing to null.

There was no error indication output in MVC. It turns out I was missing a comma in my POST body when using Postman/jQuery.

Here’s what I finally came up with to see the actual exception.

Let’s say you have an MVC action.

    [HttpPost]
    [Route("GetScore")]
    public async Task<ScoreResponse> GetScore(ScoreRequest requestContent, CancellationToken cancellationToken)
    {

add this to the body of your action method.

    // Re-wind to read the content out of the post as MVC has already read the string
    var readStream = await Request.Content.ReadAsStreamAsync();
    readStream.Seek(0, System.IO.SeekOrigin.Begin);
    var content = await this.Request.Content.ReadAsStringAsync();

    var r = JsonConvert.DeserializeObject<ScoreDto>(content);

Note: If the content is null or you get an “This stream does not support seek operations.” then your JSON object in the body must have some bad properties or missing a comma. Reduce the JSON down to the simplest form possible until you get something that is non-null.

Now in the response, you’ll get the exception (or you could try catch the DeserializeObject)

   {
    "Message": "An error has occurred.",
    "ExceptionMessage": "After parsing a value an unexpected character was encountered: \". Path 'FirstName', line 5, position 0.",
    "ExceptionType": "Newtonsoft.Json.JsonReaderException",
    "StackTrace": "   at Newtonsoft.Json.JsonTextReader.ParsePostValue(Boolean ignoreComments)\r\n   at Newtonsoft.Json.JsonTextReader.Read()\r\n   at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.PopulateObject(Object newObject, JsonReader reader, JsonObjectContract contract, JsonProperty member, String id)\r\n   at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.CreateObject(JsonReader reader, Type objectType, JsonContract contract, JsonProperty member, JsonContainerContract containerContract, JsonProperty containerMember, Object existingValue)\r\n   at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.Deserialize(JsonReader reader, Type objectType, Boolean checkAdditionalContent)\r\n   at Newtonsoft.Json.JsonSerializer.DeserializeInternal(JsonReader reader, Type objectType)\r\n   at Newtonsoft.Json.JsonConvert.DeserializeObject(String value, Type type, JsonSerializerSettings settings)\r\n   at Newtonsoft.Json.JsonConvert.DeserializeObject[T](String value, JsonSerializerSetting
   }

Other things to look for

  • you have “NULL” instead of “null” in your JSON. Note the casing!
  • public string P {get; set; } was set to null in the body of your JSON.

I hope this helps someone.



comments powered by Disqus

Please consider using Brave and adding me to your payment ledger. Then you won't have to see ads!

Support me and download Brave!

Use Brave