KendoUI Grid with MVC Wrappers and Web API

2019-07-27 10:46发布

I'm using Kendo UI grid with ASP.Net MVC Wrappers. My grid datasource is defined as follows:

.DataSource(dataSource => dataSource
        .Ajax()
            .Model(model =>
            {
                model.Id(p => p.Code);
            })
            .Read(read => read.Url("api/ProjectMilestone").Type(HttpVerbs.Get))
            .Create(create => create.Url("api/ProjectMilestone").Type(HttpVerbs.Post))
            .Update(update => update.Url("api/ProjectMilestone").Type(HttpVerbs.Put))
            .Destroy(destroy => destroy.Url("api/ProjectMilestone").Type(HttpVerbs.Delete))
      )

So one would expect that the GET url would be generated as [server]/[app]/api/ProjectMilestone.

But in my case, the page on which the grid is hosted is at the following URL: [server]/[app]/Project. This results in the GET url being generated as [server]/[app]/Project/api/ProjectMilestone, and of course the server returns error 404 not found.

Please tell me how I can have the GET url generated as [server]/[app]/api/ProjectMilestone instead.

2条回答
在下西门庆
2楼-- · 2019-07-27 11:23

Have you tried the overload that takes a Controller name and action using "api" for the controller and "ProjectMilestone" for the action?

查看更多
The star\"
3楼-- · 2019-07-27 11:32

Turns out the correct approach is to define the datasource as follows:

.Read(read => read.Url(Url.RouteUrl("DefaultApi", new { httproute ="", controller="ProjectMilestone" })).Type(HttpVerbs.Get))
.Create(create => create.Url(Url.RouteUrl("DefaultApi", new { httproute ="", controller="ProjectMilestone" })).Type(HttpVerbs.Post))
.Update(update => update.Url(Url.RouteUrl("DefaultApi", new { httproute ="", controller="ProjectMilestone" })).Type(HttpVerbs.Put))
.Destroy(destroy => destroy.Url(Url.RouteUrl("DefaultApi", new { httproute ="", controller="ProjectMilestone" })).Type(HttpVerbs.Delete))

as taken from this answer.

查看更多
登录 后发表回答