WebAPI 多个放置/发布参数 [英] WebAPI Multiple Put/Post parameters

查看:22
本文介绍了WebAPI 多个放置/发布参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在 WebAPI 控制器上发布多个参数.一个参数来自 URL,另一个来自正文.这是网址:/offers/40D5E19D-0CD5-4FBD-92F8-43FDBB475333/prices/

I am trying to post multiple parameters on a WebAPI controller. One param is from the URL, and the other from the body. Here is the url: /offers/40D5E19D-0CD5-4FBD-92F8-43FDBB475333/prices/

这是我的控制器代码:

public HttpResponseMessage Put(Guid offerId, OfferPriceParameters offerPriceParameters)
{
    //What!?
    var ser = new DataContractJsonSerializer(typeof(OfferPriceParameters));
    HttpContext.Current.Request.InputStream.Position = 0;
    var what = ser.ReadObject(HttpContext.Current.Request.InputStream);

    return new HttpResponseMessage(HttpStatusCode.Created);
}

正文的内容是JSON:

The content of the body is in JSON:

{
    "Associations":
    {
        "list": [
        {
            "FromEntityId":"276774bb-9bd9-4bbd-a7e7-6ed3d69f196f",
            "ToEntityId":"ed0d2616-f707-446b-9e40-b77b94fb7d2b",
            "Types":
            {
                "list":[
                {
                    "BillingCommitment":5,
                    "BillingCycle":5,
                    "Prices":
                    {
                        "list":[
                        {
                            "CurrencyId":"274d24c9-7d0b-40ea-a936-e800d74ead53",
                            "RecurringFee":4,
                            "SetupFee":5
                        }]
                    }
                }]
            }
        }]
    }
}

知道为什么默认绑定无法绑定到我的控制器的 offerPriceParameters 参数吗?它始终设置为空.但是我可以使用 DataContractJsonSerializer 从正文中恢复数据.

Any idea why the default binding is not able to bind to the offerPriceParameters argument of my controller? It is always set to null. But I am able to recover the data from the body using the DataContractJsonSerializer.

我也尝试使用参数的 FromBody 属性,但它也不起作用.

I also try to use the FromBody attribute of the argument but it does not work either.

推荐答案

[HttpPost]
public string MyMethod([FromBody]JObject data)
{
    Customer customer = data["customerData"].ToObject<Customer>();
    Product product = data["productData"].ToObject<Product>();
    Employee employee = data["employeeData"].ToObject<Employee>();
    //... other class....
}

使用引用

using Newtonsoft.Json.Linq;

使用 JQuery Ajax 请求

Use Request for JQuery Ajax

var customer = {
    "Name": "jhon",
    "Id": 1,
};
var product = {
    "Name": "table",
    "CategoryId": 5,
    "Count": 100
};
var employee = {
    "Name": "Fatih",
    "Id": 4,
};

var myData = {};
myData.customerData = customer;
myData.productData = product;
myData.employeeData = employee;

$.ajax({
    type: 'POST',
    async: true,
    dataType: "json",
    url: "Your Url",
    data: myData,
    success: function (data) {
        console.log("Response Data ↓");
        console.log(data);
    },
    error: function (err) {
        console.log(err);
    }
});

这篇关于WebAPI 多个放置/发布参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆