如何在Web Api中将对象作为参数传递 [英] How to pass object as parameter in Web Api

查看:184
本文介绍了如何在Web Api中将对象作为参数传递的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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.

推荐答案

不要通过GET传递对象.

Don't pass an object via GET.

发布对象并使用[HttpPost]属性.

如果您真的想通过GET进行操作,则可以使用以下方法:

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

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

这篇关于如何在Web Api中将对象作为参数传递的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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