Hello, im basically trying to do the Sorting/Paging/Filtering in my WebApi instead of the WebApp.
Im trying to send the DataSourceRequest with a Http-Request to my WebAPI which works as long I dont add a filter. If I add a Filter I get the following error
Could not create an instance of type Telerik.DataSource.FilterDescriptor. Type is an interface or abstract class and cannot be instantiated.
If my DataSourceRequest does not contain a Filter (e.g just sortings) everything works perfectly.
How I send the DataSourceRequest to my WebAPI :
var client =
new
RestClient($
"{api.Address}:{api.Port}/api/v1"
);
var rr =
new
RestRequest(
"tmp/HelloWorld"
,Method.POST);
rr.AddHeader(
"Content-Type"
,
"application/json; charset=utf-8"
);
rr.AddJsonBody(request);
// Request is the DataSourceRequest that get passed to my Method
var result = client.Execute(rr, Method.POST);
My WebApi Function
public
ActionResult<IEnumerable<TmpDTO>> HelloWorld([FromBody] DataSourceRequest request)
{
// do Something here
}
However my API never gets called when the DataSourceRequest contains a FilterDescriptor.
I saw a few threads (usually a few years old) and a few examples but I havent seen'n a single example on how to serialize the FilterDescriptors. Is this even possible? I just need the entire DataSourceRequest in my WebAPI
Thanks in advance.
7 Answers, 1 is accepted
Hello Steven,
Could you please confirm if you have the DataSource WebApi() type set in the configuration at your end?
.DataSource(dataSource =>
dataSource
.WebApi()
.Model(model =>
{
model.Id(p => p.ProductID);
})
.Events(events => events.Error("error_handler"))
.Read(read => read.Action("Get", "Product"))
.Create(create => create.Action("Post", "Product"))
.Update(update => update.Action("Put", "Product", new { id = "{0}"} ))
.Destroy(destroy => destroy.Action("DELETE", "Product", new { id = "{0}" }))
)
I would suggest you take a look at the Grid Web Api binding demo
You could also take a look at the following post in our Forum thread where serialization and deserialization of FilterDscriptor is discussed:
- https://www.telerik.com/forums/reset-grid-to-initial-state#2PRJHbkesUakT38wFDnjhQ
I hope the provided information will be helpful.
Regards,
Neli
Progress Telerik
Virtual Classroom, the free self-paced technical training that gets you up to speed with Telerik and Kendo UI products quickly just got a fresh new look + new and improved content including a brand new Blazor course! Check it out at https://learn.telerik.com/.
Hello Neli,
thanks for your answer. WebApi() is set in my grid configuration. As I said everything works perfectly fine as long as the DataSourceRequest does not contain any FilterDescriptor. A DataSourceRequest with Paging and sortings e.g does work perfectly fine and my asp.net core webapi receives the request with the DataSourceRequest. However if I try to add a Filter I get the error message thats written in my first post.
Im adding the DataSourceRequest with the following code to my post request
rr.AddJsonBody(request);
Does "RestRequest(..).AddJsonBody(datasourcerequestVariable)" even work when having filters? For me it sounds like he cannot convert the FilterDescriptors to JSON for w/e reason. Am I missing something?
Neither the example or the forum disccusion helped me. For me it sounds it should work when using an asp.netcore webapp and a asp.net core api.
Hello Steven,
I have tested locally by sending an ajax request with a custom filter to the web API endpoint. The data was filtered and returned without any issues. (Note, that I was not using the RestClient).
I noticed that you are using the [FromBody] attribute in the endpoint. Could you please try to parse the result by using the [DataSourceResult] attribute in the controller and let me know about the result? You could also take a look at the endpoint example in the following link.
Also, in the provided snippet the post request is used. By design, get request needs to be used for fetching data with the WebApi.
Regards,
Neli
Progress Telerik
Virtual Classroom, the free self-paced technical training that gets you up to speed with Telerik and Kendo UI products quickly just got a fresh new look + new and improved content including a brand new Blazor course! Check it out at https://learn.telerik.com/.
Hello Neli, thanks for your answer and sorry for the late reply.
Unfortunately, it still doesnt work me. Why is every example basically just one app? Basically every example is just the view calling a method on the webapp controller itself (and doing db stuff there etc). Its just as an example right? This does work on a seperate Asp.Net core Webapi right? Because my project wont recognize [DataSourceRequest] as an attribute ("DataSourceRequest is not an attribute"). Thy only telerik nugget package I included is "Telerik.DataSource". Do I really have to include the entire UI nugget packages in my WebApi to make it work? Is there maybe an example with a seperate WebApp and a seperate WebApi? Because I've tried literally everything you just suggested. Nothing works, the only way I get the DataSourceRequest to the webapi itself is with my implementation but it will still crash if theres a filter in it.
Greetings
Hi Steven,
I am afraid that we do not have a runnable example of separate WebApp and WebApi.
Indeed, In order to use the [DataSourceRequest] a reference to the Kendo.Mvc.UI should be included, as it is described for example in the Grid Custom binding article linked here. By using the DataSourceRequest and ToDataSourceResult() all data operations like paging, filtering, grouping, and sorting are performed "automatically" behind the scenes. Thus, I would recommend to include Kendo.MVC.UI in your project.
I have tested locally in a sample project that you will find attached. In the project on a button click a request to the WebApi endpoint is sent. The applied filter is received in the remote endpoint without any errors. The filtered data is console logged. Below is the respective endpoint, that you will find in the ‘GridController.cs’ file:
[HttpGet]
public ActionResult Get([FromBody][DataSourceRequest]DataSourceRequest request)
{
return Json(service.Read().ToDataSourceResult(request));
}
I have tested also by filtering a Grid data as demonstrated below:
var grid = $("#grid").data("kendoGrid");
grid.dataSource.filter({ field: "ProductName", operator: "contains", value: "5" })
In case the issue continues to persist, please send us an isolated runnable sample where the described issue is reproduced. This will enable us to examine it locally and look for its cause.
Regards,
Neli
Progress Telerik
Virtual Classroom, the free self-paced technical training that gets you up to speed with Telerik and Kendo UI products quickly just got a fresh new look + new and improved content including a brand new Blazor course! Check it out at https://learn.telerik.com/.
Hi Neli,
I have the same doubt of how to manually send DataSourceRequest through, like using RestClient. The reason Steven's code failed is because is because FilterDescriptor is like a binary and failed to serialize to JSON.
Do you have an example to send DataSourceRequest like using RestClient manully?
Hi Gang,
I am afraid we do not have an example of sending the DataSourceRequest with RestClient.
I would like to note that currently, serialization and deserialization of the DataSourceRequest are not supported. Below you will find a link to a feature request for adding serialization support of DataSourceRequest in ASP.NET MVC:
I would suggest you cast a vote for the request as the more popular a request gets, the higher its chances of getting approved for implementation.
Regards,
Neli
Progress Telerik
Love the Telerik and Kendo UI products and believe more people should try them? Invite a fellow developer to become a Progress customer and each of you can get a $50 Amazon gift voucher.