How to pass object as parameter in Web Api

早过忘川 提交于 2019-12-10 23:04:02

问题


I want to pass object as parameter in my web api GET and POST method. My code is,

    [HttpGet]
    [Route("mytest/list/{model}")]
    public IHttpActionResult GetAllTypes(TestModel model)
    {
        //my logic here
    }

When I call this, console get an error its not found. I tried this,

   [HttpGet]
    [Route("mytest/list/{model?}")]
    public IHttpActionResult GetAllTypes(TestModel model)
    {
        //my logic here
    }

But, the parameter object gets null value.


回答1:


Don't pass an object via GET.

POST an object and use [HttpPost] attribute.

If you're really want to do it via GET , you can use this :

 [HttpGet]
 public IHttpActionResult GetAllTypes([FromUri] TestModel model)


来源:https://stackoverflow.com/questions/43538171/how-to-pass-object-as-parameter-in-web-api

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!