1

I'm creating a RESTful API with ASP.Net MVC
Have problem with one GET route. I created a parameters model to handle all receiving params.

 public class MyParameters : BaseParameters
 {
    public string ProductCode { get; set; }
    ...
 }

and using this class in this way:

 public HttpResponseMessage Get([FromUri] MyParameters filter)  

The problem is when i don't pass any parameters - the filter object not being initialized. It is always null.
But I need this object to be initialized anyway, because there are some default parameters, like flags, which I initialize in constructor.

 public MyParameters()
    {
        AuthFlag = 'Y';
    }

I know that it is default behaviour, but I don't want to do something like this:

if (filter == null)
      filter = new MyParameters();

Maybe Is there any configuration available to make parameters objects created anyway?

Thank you for any suggestions.

1 Answers1

5

There's the null propagation operator:

filter = filter ?? new MyParameters();

or you can implement your own model binder/model binder provider. As a starting point, you can reference this.

Moho
  • 15,457
  • 1
  • 30
  • 31