【问题标题】:Reading the parameters sent in the body from applet to the post method of web api controller从小程序读取body中发送的参数到web api控制器的post方法
【发布时间】:2012-09-11 10:47:09
【问题描述】:

考虑下面的类

public class StController:apicontroller {

    public void PostBodyMethod() {
        HttpRequestMessage request=this.request;
     //How to read the header and body parameters
    }
}

applet 将 header 和 body 参数都发送到 post 方法。 如何使用 HttpRequestMessage 对象在 webapi 控制器内部检索与 post 方法一起发送的信息?

【问题讨论】:

    标签: asp.net json asp.net-mvc-3 asp.net-web-api json.net


    【解决方案1】:

    如果 body 参数是一个 JSON 对象,你只需要在 Post 方法中传递 Model 参数。 Web API 默认支持 json。你可能需要阅读this

    对于 HttpRequest 中的阅读器标头,您可以使用:

    var headers = ControllerContext.Request.Headers;
    

    示例代码:

    class Model 
    { 
        public int Id { get; set; } 
        public int Hj { get; set; }  
    } 
    
    public class StController : ApiController { 
    
        public void Post(Model model) { 
    
         //How to read the header and body parameters
         var headers = ControllerContext.Request.Headers;
        } 
    }
    

    【讨论】:

    • 问题是我从小程序调用 post 方法,而小程序正在以无参数方法发送标头和正文中的参数,例如:Post()。我必须强制使用 HttpRequestMessage 对象。只是这个我不知道怎么用,能指导我怎么用吗?
    • 为什么提到body参数是一个类的JSON对象
    • 实际上它是 Java 使用的自定义类,它们以键值对的形式发送值,例如 {Id:1,hj:2,Id:2,hj:3}...Along有了它,他们还发送了诸如 name="abcd" 之类的标头参数......那么现在在我的 apicontroller 中检索这些值的方法是什么?
    • @user1599992:请您更新您的问题以获得更正确的信息吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-10-02
    • 1970-01-01
    • 2017-05-17
    • 1970-01-01
    • 2019-04-08
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多