How to properly use Telerik / Kendo MVC routing with API calls

52 Views Asked by At

I've been using the Kendo MVC controls with WebAPIs but it has always irked me that I can't utilise the URL routing to what I would like.

If the list's contents are static and always points to one URL endpoint, then this isn't an issue because the URL will be defined once in the beginning.

However, I have a case where the URL parameters need to change dynamically where it's list depends on a selection from another list.

For example I have 2 controllers, Users and Books.

I want to be able to get a list of all the books the user currently has.

The Users controller has a method with route:

/api/User/{userId}/Books

And the Books controller has a method with route:

/api/Books/User

Ideally, I would like to use the User controller and pass in the User ID and get all the books the user has access to.

However, with my current implementation, I have to use the Books Controller and pass in a data parameter with the User ID instead - which does the job but it's not very clean.

This is what the datasource looks like:

.DataSource(dataSource => dataSource
    .WebApi()
    .PageSize(100)
    .Batch(true)
    .ServerOperation(true)
    .Read(read => read.Url("api/Books/User")
        .Data("bookData"))
)

The Data method passes in the UserId parameter to the end of the query so it becomes:

/api/Books/User?UserId=120

which works as a work around.

I have tried changing the route to be:

/api/User/0/Books

to see if this will call the correct endpoint, and while it does call the User controller now and get the correct data, the control is broken because it tries to make another call where for some reason it has a page=NaN.

(And also the above route still gives the URL to be /api/User/0/Books?....&userId=120 instead of /api/user/120/books anyway.)

Has anyone had any luck trying to get the Kendo controls to use dynamic routing as expected?

0

There are 0 best solutions below