的WebAPI多认沽/后置参数 [英] WebAPI Multiple Put/Post parameters

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

问题描述

我想发布多个参数上的WebAPI控制器。一个参数是从URL,另一个从主体。下面是网址:   /报价/ 40D5E19D-0CD5-4FBD-92F8-43FDBB475333 /价格/

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/

下面是我的控制器code:

Here is my controller code:

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 的说法?它总是被设置为null。但我能够使用来恢复身体数据的 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.

推荐答案

编辑:如果您使用的WebAPI 2(希望你,如果你正在读这篇文章后,我做了这个编辑)看<一href="http://www.asp.net/web-api/overview/formats-and-model-binding/parameter-binding-in-aspnet-web-api">http://www.asp.net/web-api/overview/formats-and-model-binding/parameter-binding-in-aspnet-web-api.

If you are using WebAPI 2 (and hopefully you are if you're reading this after I've made this edit) see http://www.asp.net/web-api/overview/formats-and-model-binding/parameter-binding-in-aspnet-web-api.

您不能做到这一点的的WebAPI。请参阅<一href="http://www.west-wind.com/weblog/posts/2012/May/08/Passing-multiple-POST-parameters-to-Web-API-Controller-Methods">http://www.west-wind.com/weblog/posts/2012/May/08/Passing-multiple-POST-parameters-to-Web-API-Controller-Methods进行了详细的了解一下吧,还有一些有用的变通。

You can't do that with WebAPI. See http://www.west-wind.com/weblog/posts/2012/May/08/Passing-multiple-POST-parameters-to-Web-API-Controller-Methods for a detailed look at it, along with some useful work-arounds.

如果你搜索自体,你还可以找到注释专门处理您的替代尝试。

If you search for "from body" you'll also find a comment dealing specifically with your alternate attempt.

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

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