2

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.

3
  • What does TestModel look like? Commented Apr 21, 2017 at 8:58
  • Test model contains, public int CurrentPage { get; set; } public int ItemsPerPage { get; set; } public int PageNumber { get; set; } public string Type { get; set; } and more.. Commented Apr 21, 2017 at 9:01
  • 1
    You try to put an Object into an Url Route. But a URL is nothing other than a string, so you can't put it in there. What you can do is encode it with JSON, but that won't work in the URL because of the encoding of the characters. You can either do a Post (If it doesn't have to be restful) or take it in via FromUrl, as Royi described. Commented Apr 21, 2017 at 9:01

1 Answer 1

7

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)
Sign up to request clarification or add additional context in comments.

4 Comments

yes, you are correct, but actually this method get some value, its based some parameter condition. my object contains more than 10 parameters.
@Jinesh Like I've written - did you try [FromUri] ?
There is some limitation of no of character in URL and get request always pass data in URL using query string. So if object has more number of properties it can't pass values to server.
@arpandesai Right. That's why you should think REST. url:80/api/types/get/categorynum. Not to mention that GET can/is cached.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.