WebAPI Selfhost: Can't bind multiple parameters to the request's content

前端 未结 2 1390
小蘑菇
小蘑菇 2021-01-01 09:24

The below code are simplified to show the necessity. May I know what is wrong? I can\'t seems to retrieve two Parameters (A and B in this case) using the [FromBody] attribut

相关标签:
2条回答
  • 2021-01-01 09:30

    Web Api doesn't support multiple [FromBody] params I think. But you can use Api model, to passing more parameters to your api action.:

    public class YourApiModel
    {
        public int A{ get; set; }
    
        public int B { get; set; }
    
        //...other properties    
    }
    

    After that, you can simply use this in your API controller Test:

        // POST: api/test
        public IHttpActionResult Post([FromBody] YourApiModel model)
        {
            //do something
        }
    

    Hope it help.

    0 讨论(0)
  • 2021-01-01 09:52

    Try the Web API code:

    [DataContract]
    public class Model
    {
        [DataMember]
        public int A { get; set; }
    
        [DataMember]
        public int B { get; set; }
    }
    
    [Route("API/Test"), HttpPost]
    public IHttpActionResult Test([FromUri] Model model)
    
    0 讨论(0)
提交回复
热议问题