1

I'm using .net core Web API. I'm posting generic QueryFilter class from angular. But, generic part of class return null. When, i change to parameter like this, it is working perfectly :

 public async Task<ServiceResult> GetStudentsForGrid([FromQuery]QueryFilter queryFilter,[FromQuery]StudentFilter studentFilter)
 { } //This working perfectly

QueryFilter.cs

public class QueryFilter<T> where T : class
{
    public string SortBy { get; set; }
    public bool IsSortAscending { get; set; }
    public int PageFirstIndex { get; set; }
    public byte PageSize { get; set; }
    public T CustomFilter { get; set; }
}

StudentFilter.cs

public class StudentFilter
{
    public string Name { get; set; }
    public string Surname { get; set; }
}

Controller.cs (not working)

 [HttpGet("GetStudentsForGrid")]
 public async Task<ServiceResult> GetStudentsForGrid([FromQuery]QueryFilter<StudentFilter> queryFilter)
 { } //This not working

I don't want to send every time two parameter. So, I want to use generic way. How can i fix this null exception?

2
  • What does your request URI end up looking like? I'm not sure how the binding mechanism is supposed to figure out what goes where when you have a nested model and a query string that is by definition "flat", so to speak. Did you try looking into model binders? Just for fun, what happens if you make it a POST and read the model from the JSON request body you send from JS? Commented Oct 5, 2018 at 7:23
  • @itminus answer worked perfectly. Thanks. Commented Oct 5, 2018 at 7:42

1 Answer 1

1

If you want to use a "generic" way , let's say your controller action is :

public IActionResult GetStudentsForGrid([FromQuery]QueryFilter<StudentFilter> queryFilter)
{ 
    var x= queryFilter;
    return new JsonResult(x);
} 

you have to sent the request with a well formatted querystring :

GET https://localhost:5001/api/students?sortBy=Hello&pageSize=10&customFilter.Name=1&customFilter.SurName=2 HTTP/1.1

Note the way we pass the parameters of customFilter.Name and customFilter.SurName.

The response will be :

HTTP/1.1 200 OK
Content-Type: application/json; charset=utf-8
Server: Kestrel
Transfer-Encoding: chunked

{
  "sortBy": "Hello",
  "isSortAscending": false,
  "pageFirstIndex": 0,
  "pageSize": 10,
  "customFilter": {
    "name": "1",
    "surname": "2"
  }
}
Sign up to request clarification or add additional context in comments.

4 Comments

Thanks a lot @itminus. It worked perfectly. My mistake is; I am posting parameter as "Name" not "customFilter.Name". But, when i used two parameter (non-generic way), c# automatically bind without "customFilter.Name". So, I thinked in generic way will work. But Ilearnt that, generic class is different from normal class. In generic way, i must send as "customFilter.Name" not "Name".
But in your answer, you convert my original method from "public async Task<ServiceResult> GetStudentsForGrid" to "public IActionResult GetStudentsForGrid". But, it is not necessary. It's also working async Task.
@HasanOzdemir That's because I don't have the definition of ServiceResult, so I just take the IActionResult as an example to test with PostMan to make sure I'm doing the right thing , and also , it will make my example more clean . :)
You are right.This is more claner. Thanks again @itminus

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.