Biarity / Sieve

⚗️ Clean & extensible Sorting, Filtering, and Pagination for ASP.NET Core
Other
1.21k stars 131 forks source link

"The input was not valid." #39

Open sphildreth opened 6 years ago

sphildreth commented 6 years ago

I try to use the default code using ASP.NET Core 2.1 Web API and anytime I put SieveModel as a post parameter I get "The Input was not valid." as a result.

        [HttpGet]
        public ActionResult<IEnumerable<Publisher>> Get(SieveModel sieveModel)
        {
            var data = this._repository.List();
            return _sieveProcessor.Apply(sieveModel, data).ToArray();
        }
Biarity commented 6 years ago

That's quite odd, what happens if you look at the contents of sieveModel? Have a look at this for a working project.

diezsenn commented 5 years ago

This should work

        [HttpGet]
        public ActionResult<IEnumerable<Publisher>> Get([FromQuery] SieveModel sieveModel)
        {
            var data = this._repository.List();
            return _sieveProcessor.Apply(sieveModel, data).ToArray();
        }
a-patel commented 5 years ago

@sphildreth Please make sure below configuration,

services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_1); // Give me the 2.1 behaviors

bugproof commented 5 years ago

use @diezsenn code, asp.net core MVC tries to bind classes(complex types) from the body by default, you need to add [FromQuery] before.

Few other tips: use sieve directly on IQueryable if you use ORM, then it will translate it to appropriate SQL query, use async methods for DB calls and I/O calls. If you have repository abstraction created, use Sieve inside your repository, it is repository responsibility, to filter, sort and paginate.

@Biarity I think it would be reasonable to provide custom IBindingMetadataProvider to tell ASP.NET Core MVC to bind SieveModel from query by default globally so people won't forget to use [FromQuery] see: https://docs.microsoft.com/en-us/aspnet/core/mvc/models/model-binding?view=aspnetcore-2.1#customize-model-binding-and-validation-globally but don't do it if it adds dependency, then optionally some nuget package for MVC could be provided

pangjianxin commented 5 years ago

would you add an asynchronous version for Apply method in the SieveProcessor class?