我从一个asp.net页面发送来自javascript的Json数据,JS数据/对象先前被序列化并发送到我的asp.net genericHandler.ashx页面。
在服务器端我使用的是Json.net,这是我在通用处理程序中的代码:
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);
我的反序列化对象类
public class MyData
{
public string name { get; set; }
public string lastName { get; set; }
public string age { get; set; }
}
这是我得到的错误。
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.
我已经尝试了内置的.net Json序列化程序(JavaScriptSerializer),它可以正常工作,但是对于Json.net,我收到了这个错误。我该如何解决?