如何通过 ASP.Net context.Request 检索 JSON

发布于 2024-09-12 20:37:13 字数 559 浏览 3 评论 0原文

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"
 });

我试图通过此代码在 ASHX 文件中的服务器端检索发布的 JSON 数据:

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

但它总是返回 null。我做错了什么?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

孤云独去闲 2024-09-19 20:37:13
  1. HttpContext.Current.Request.InputStream 获取请求正文
  2. 读取输入流并转换为字符串
  3. 使用 javascriptserializer 将 json 对象反序列化为强类型对象(确保 json 属性与强类型对应部分共享相同的名称)
  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)
终难愈 2024-09-19 20:37:13

来自 http://dailydotnettips.com/2013/ 09/26/从jquery发送原始json请求到asp-net/

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));

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));
↘人皮目录ツ 2024-09-19 20:37:13

挖掘互联网。我发现 IE 在接收完整的 POST 请求时出现问题。 @ronaldwidha 对 InputStream 的建议与我发现的类似。但我没有使用 javascriptserializer,而是使用 JSON.NET 代码片段如下,我希望这能帮助其他遇到类似问题的人

 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;
   }
}

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;
   }
}
只是我以为 2024-09-19 20:37:13

Request[] 只会查看表单参数和 quetystring。您将需要执行表单发布或使用 qs 或自己解析请求正文。

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.

蘑菇王子 2024-09-19 20:37:13

我认为您可以从 HttpCurrent.Context.Request.GetResponse() 中获取请求正文。

首先验证内容类型标头可能是个好主意。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文