0

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.

enter image description here

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?

Carlo Luther
  • 2,402
  • 7
  • 46
  • 75
  • What is `contentType` (without the `2` suffix) - as shown in debug window? – EdSF Aug 12 '14 at 23:36
  • It's the contentype object associated with MyData class, I've used with .net JavaScriptSerializer, and it worked. – Carlo Luther Aug 12 '14 at 23:39
  • I've just commented out the code above the one I've posted, related to the built in JavaScriptSerializer, and it works, how come? I've got no idead, Is it because I've applied new StreamReader(context.Request.InputStream) twice? – Carlo Luther Aug 12 '14 at 23:42
  • 3
    `Streamreader`: [StackOverflow](http://stackoverflow.com/a/21972422/304683) and [MSDN](http://msdn.microsoft.com/en-us/library/system.io.streamreader(v=vs.110).aspx) in Remarks-> Important section. Notice the empty string `json` variable. Also, you already read the stream in `jsonString`. Hth... – EdSF Aug 12 '14 at 23:56
  • @EdSF yep, that was the issue. – Carlo Luther Aug 13 '14 at 11:41
  • Based on a question on StackOverflow, the details of failure in using StreamReader twice or more, is that data gets/calls Dispose after the stream has been read. The best solution is to use a buffer. http://stackoverflow.com/questions/21971467/why-cant-i-read-http-request-input-stream-twice – Carlo Luther Aug 13 '14 at 11:49

0 Answers0