KendoUI Grid with MVC Wrappers and Web API

余生长醉 提交于 2019-12-24 12:12: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.


回答1:


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.




回答2:


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



来源:https://stackoverflow.com/questions/19841453/kendoui-grid-with-mvc-wrappers-and-web-api

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!