简体   繁体   English

如何从post方法接收Web API控制器中的参数?

[英]How to receive parameters in Web API controller from post method?

Net based application using Web api and angularjs. 使用Web api和angularjs的基于网络的应用程序。 I am trying to integrate payment API's. 我正在尝试集成支付API。 The post variables can be accessed on the redirect URL. 可以在重定向URL上访问post变量。

I am able to receive in asp.net as below. 我可以在asp.net中收到如下信息。

 protected void Page_Load(object sender, EventArgs e)
        {
            string order_id = Request.Form["order_id"];
            string transaction_id = Request.Form["transaction_id"];
        }

I want to receive parameters in Webapi2. 我想在Webapi2中接收参数。 Is there any way i can write above piece of code in api? 有什么办法可以在api中编写以上代码? Thanks for your help. 谢谢你的帮助。

You can post your data from AngularJs app to Web API using the $http.post method. 您可以使用$ http.post方法将数据从AngularJs应用发布到Web API。

var data = {
    orderId: your_order_id,
    transactionId: your_transactio_id
};

$http
    .post("/api/token", data)
    .then(onSuccess, onError);

If you are passing data in post inside body, you should be able to receive the data using 如果您要在身体内部传递数据,则应该能够使用

public bool Post(PostParameters parameters)
{
    var orderId = parameters.OrderId;
    var transactionId = parameters.TransactionId;
}        

Where PostParamaters is just a plain class with the properties you want to pass. 其中PostParamaters只是具有您要传递的属性的普通类。 Of course, your data types can be different. 当然,您的数据类型可以不同。

public class PostParameters
{
    public int OrderId { get; set; }
    public string TransactionId { get; set; }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM