1

In my WebAPI I have model

 public class ListRequest 
 {
    public int Skip { get; set; } = 0;
    public int Take { get; set; } = 30;
 }

My action is

 [HttpGet]
 [Route("api/users")]
 public IHttpActionResult Get([FromUri] ListRequest request) {
    ...
 }

I need to have possibility to not pass any query parameters, then default values should be used. But, when I go to http://localhost:44514/api/users the request is null. If I remove [Route("api/users")] then request is not null and has default values for parameters.

How can I reach that behavior with Route attribute?

Pr.Dumbledor
  • 636
  • 1
  • 11
  • 29
  • 4
    Why do you have the `ListRequest request` parameter when your not passing any query string values? (you may as well just initialize a new instance in the method) –  Jul 21 '17 at 10:26
  • @StephenMuecke I'm not passing any parameters when I want to use default parameter values. Before I add attribute everything works fine, but I need use that attribute – Pr.Dumbledor Jul 21 '17 at 11:25
  • 2
    You may try using [HttpPost] method to pass a model in the request as a parameter. – Dalvinder Singh Jul 21 '17 at 11:34
  • 1
    @DalvinderSingh No, it is GET. Model used for filtering, some time other model can has over 5 properties to filter. So I just want GET with filter. And I use model instead of many parameters of action – Pr.Dumbledor Jul 21 '17 at 11:37
  • You can find similar question like this here.. https://stackoverflow.com/questions/24075892/how-to-bind-a-request-model-in-webapi-get-request-with-route-attribute – Dalvinder Singh Jul 21 '17 at 11:54
  • @DalvinderSingh But I've already had FromUri – Pr.Dumbledor Jul 21 '17 at 12:05
  • @DalvinderSingh thank for your help and advices. I solved that – Pr.Dumbledor Jul 21 '17 at 12:43

3 Answers3

2

If you want to init model using Route attributes try

Route("api/users/{*pathvalue}")]
Roman Marusyk
  • 23,328
  • 24
  • 73
  • 116
0

Create your method on post request basis. Get type always receive null value.

[HttpGet]
[Route("api/users")]
public IHttpActionResult Get([FromUri] ListRequest request) {

}

Change to

[HttpPost]
[Route("api/users")]
public IHttpActionResult Get([FromUri] ListRequest request) {
...
}

Because Model (Class) type parameter does not support get type request.

Hope it will help.

Maveňツ
  • 1
  • 12
  • 50
  • 89
Raja
  • 152
  • 8
  • I can't change it. Model used for filtering, some time other model can has over 5 properties to filter. So I just want GET with filter. And I use model instead of many parameters of action – Pr.Dumbledor Jul 21 '17 at 12:32
  • anything else. Get request wont support model type parameter. You need to change according to request basis not your requirement basis. – Raja Jul 21 '17 at 12:36
  • Why? Where did you find that Get request wont support model type parameter. I always use it. And see the question. It works but only without attribute – Pr.Dumbledor Jul 21 '17 at 12:37
  • Also you can see the comment below the question. We have already talk about that with DalvinderSingh. So your answer is not helpful. sorry – Pr.Dumbledor Jul 21 '17 at 12:40
0

Use data annotation. For more information visit Default value in mvc model using data annotation

Change

public class ListRequest 
{
    public int Skip { get; set; } = 0;
    public int Take { get; set; } = 30;
}

To

    public class ListRequest 
    {
        [DefaultValue(0)]
        public int Skip { get; set; }
        [DefaultValue(30)]
        public int Take { get; set; }
    }

It works without removing [Route("api/users")] and request will not be null.

dovar21
  • 1
  • 1