How to read postdata in a ashx webHandler ASP.NET 3.5

923 views Asked by At

I am trying to acces post data in an ashx webhandler, in an MVC project I used Newtonsoft.Json to achieve this.

Dictionary<string, string> postData = JsonConvert.DeserializeObject<Dictionary<string, string>>(data);

this resulted in a dictionary where i can acces the post data with

  if (postData.ContainsKey("myKey"))
    {
        someVar = postData["myKey"];
    }

In another project ASP.NET 3.5 I don't seem to have Newtonsoft available and I try to use:

object postData = new JavaScriptSerializer().DeserializeObject(data);

This method is supposed to return an object graph, and it seems to work fine, if I look with debugger in visual studio, I can see the data that I posted:

enter image description here

How can I access the data in this object?, if I type postData. the only options i get are: Equals, GetHashCode, GetType and ToString

I tried postData[0].value;

but this gives an error: cannot apply indexing.

2

There are 2 answers

1
Andrei On BEST ANSWER

DeserializeObject outputs a dictionary, so you should be able to do this:

var postData = new JavaScriptSerializer().DeserializeObject(data) as Dictionary<string, object>;
var filename = postData["file"];
0
gunvant.k On

try to use custom type instead of object and see if its works

 public class yourKeyvalue
    {
        public string _Key{ get; set; }
        public string _value{ get; set; }

    }


    var postData = new JavaScriptSerializer().Deserialize<yourKeyvalue>(data);
    var Key = postData._Key 
    var Value = postData._value