开发者

How to retrieve JSON via ASP.Net context.Request

开发者 https://www.devze.com 2023-01-10 05:50 出处:网络
var OrderInfo = {\"ProductID\": \"ProductIDValue\", \"ProductName\": \"ProductName\", \"Quantity\": 1, \"Amount\": 9999,
var OrderInfo = {"ProductID": 
    "ProductIDValue",
    "ProductName": "ProductName",
    "Quantity": 1,
    "Amount": 9999,
    "SLQuantity": 9999,
    "SLDate": "08/03/2010"
};

var DTO = { 'OrderInfo': OrderInfo };
$.ajax({
    开发者_开发技巧type: "POST",
    contentType: "application/json; charset=utf-8",
    url: "JasonHandler.ashx",
    data: JSON.stringify(DTO),
    dataType: "json"
 });

I'm trying to retrieve posted JSON data on server side in an ASHX file via this code:

string strrequest = context.Request["OrderInfo"];

but it always return null. What Am I doing wrong?


  1. get the request body from HttpContext.Current.Request.InputStream.
  2. read the input stream and convert to string
  3. use javascriptserializer to deserialize the json object to a strongly type object (ensure the json properties share the same name as the strongly type counter part)


From http://dailydotnettips.com/2013/09/26/sending-raw-json-request-to-asp-net-from-jquery/

var jsonString = String.Empty;

context.Request.InputStream.Position = 0;
using (var inputStream = new StreamReader(context.Request.InputStream))
{
jsonString = inputStream.ReadToEnd();
}

JavaScriptSerializer javaScriptSerializer = new JavaScriptSerializer();
object serJsonDetails = javaScriptSerializer.Deserialize(jsonString, typeof(object));


Digging the Internet. I found out that IE has problem receiving POST request in full. @ronaldwidha's suggestion on InputStream is similar to what I have found. But rather than using javascriptserializer I use JSON.NET Code snippets is below and I hope this would help other with similar problem

 public class JasonHandler : IHttpHandler {

 public void ProcessRequest (HttpContext context) {

    context.Response.ContentType = "application/json";
    context.Response.ContentEncoding = Encoding.UTF8;

    System.IO.Stream body = context.Request.InputStream;
    System.Text.Encoding encoding = context.Request.ContentEncoding;
    System.IO.StreamReader reader = new System.IO.StreamReader(body, encoding);
    if (context.Request.ContentType != null)
    {
        context.Response.Write("Client data content type " + context.Request.ContentType);
    }
    string s = reader.ReadToEnd();
    string[] content = s.Split('&');
    for (int i = 0; i < content.Length; i++)
    {
        string[] fields = content[i].Split('=');
        //context.Response.Write("<div><strong>" + fields[0] + "</strong></div>");
        //context.Response.Write("<div>" + fields[1] + "</div> ");  
    }

    string jsonRecord = s;
   }
}


Request[] will only look at form params and quetystring. You will need to do a form post or use qs or parse the request body yourself.


I think you could get the request body out of HttpCurrent.Context.Request.GetResponse().

Its probably a good idea to verify the content-type header first.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号