代码之家  ›  专栏  ›  技术社区  ›  YOUNG MIN CHO

如何使用ASP。NET 4.0 Web窗体

  •  0
  • YOUNG MIN CHO  · 技术社区  · 5 年前

    我试图在ASP.NET中获取原始请求体。NET 4.0 Web窗体。
    请求[“param”],请求。表格[“param”],请求。QueryString[“param”]不起作用。
    知道如何获得这些参数吗?

    enter image description here

    // API POST Request with this Request Body (raw-json)
        {
          "name" : "Apple",
          "image" : "https://upload.wikimedia.org/wikipedia/commons/thumb/1/15/Red_Apple.jpg/265px-Red_Apple.jpg",
          "price" : 35
        }
    
    // API Server trying to get request body.
    
    // didn't work. empty
    Request["name"]
    // didn't work. empty
    Request.Form["name"]
    // didn't work. empty
    Request.QueryString["name"]
    
    
    1 回复  |  直到 5 年前
        1
  •  2
  •   Yehor Androsov    5 年前

    您可以使用 Request.InputStream

    // include this in the top of your page to use JavaScriptSerializer and Hashtable
    using System.Web.Script.Serialization;
    using System.Collections;
    
    ...
    using (var sr = new StreamReader(Request.InputStream))
    {
        string body = sr.ReadToEnd();
        
        // Deserialize JSON to C# object
        // you can use some modern libs such as Newtonsoft JSON.NET instead as well
        JavaScriptSerializer serializer = new JavaScriptSerializer();
        Hashtable hashtable = serializer.Deserialize<Hashtable>(body);
    
        string name = hashtable["name"].ToString();
        string image = hashtable["image"].ToString();
        string price = hashtable["price"].ToString();
    
    }
    
    
    推荐文章