如何在 Web Api 中将对象作为参数传递

2024-05-06

我想在我的 web api GET 和 POST 方法中将对象作为参数传递。我的代码是,

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

当我调用它时,控制台收到错误,未找到。我试过这个,

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

但是,参数对象获取空值。


不要通过 GET 传递对象。

POST 对象并使用[HttpPost]属性。

如果你真的想通过 GET 来做到这一点,你可以使用这个:

 [HttpGet]
 public IHttpActionResult GetAllTypes([FromUri] TestModel model)
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

如何在 Web Api 中将对象作为参数传递 的相关文章

随机推荐