I’m sending from an asp.net page Json data from javascript, the JS data/object was previously serialized and the sent to my asp.net genericHandler.ashx page.
On server side I’m using Json.net, this is my code in the generic handler:
public void ProcessRequest(HttpContext context)
{
int numberOfFiles = 0;
MyGlobalSettings myGlobalSett = new MyGlobalSettings();
JavaScriptSerializer jsonSerializer = new JavaScriptSerializer();
string jsonString = string.Empty;
context.Request.InputStream.Position = 0;
using (StreamReader inputStream = new System.IO.StreamReader(context.Request.InputStream))
{
jsonString = inputStream.ReadToEnd();
}
MyData contentType = new MyData();
contentType = jsonSerializer.Deserialize<MyData>(jsonString);
Debug.WriteLine("Context 1:" + contentType.name);
context.Request.InputStream.Position = 0;
string json;
using (var reader = new StreamReader(context.Request.InputStream))
{
json = reader.ReadToEnd();
}
MyData contentType2 = new MyData();
contentType2=JsonConvert.DeserializeObject<MyData>(json);
Debug.WriteLine("Context 2:" + contentType2.name);
My deserialized object class
public class MyData
{
public string name { get; set; }
public string lastName { get; set; }
public string age { get; set; }
}
This is the error I get.
An exception of type 'System.NullReferenceException' occurred in Demo_xss_prevention_04.dll but was not handled in user code
Additional information: Object reference not set to an instance of an object.
I've tried the built in .net Json serializer (JavaScriptSerializer), and it works, but with Json.net I get this error. How can I fix it?