我正在研究.net web api .....
Web Api配置:
public static void Register(HttpConfiguration config)
{
// Verb Routing
RouteTable.Routes.MapHttpRoute(
name: "SmallBizApi",
routeTemplate: "api/{controller}/{action}/{id}",
defaults: new
{
id = RouteParameter.Optional,
action = RouteParameter.Optional
}
);
config.Formatters.Clear();
config.Formatters.Insert(0, new SmallBiz.WebAPI.Common.JsonpFormatter());
}
我正在使用jsonp格式在kendo-ui甘特图中加载数据,可以看出......
<div id="grid"></div>
<script src="http://code.jquery.com/jquery-1.9.1.min.js"></script>
<script>
$(document).ready(function () {
var projectdata = "http://localhost:1799/api",
dataSource = new kendo.data.DataSource({
transport: {
read: {
url: projectdata + "/project",
dataType: "jsonp"
},
update: {
url: projectdata + "/project/put",
dataType: "jsonp"
},
destroy: {
url: projectdata + "/project/delete",
dataType: "jsonp"
},
create: {
url: projectdata + "/project/post",
dataType: "jsonp"
},
parameterMap: function (options, operation) {
if (operation !== "read" && options.models) {
return { models: kendo.stringify(options.models) };
}
}
},
batch: true,
pageSize: 20,
schema: {
model: {
id: "ProjectId",
fields: {
ProjectId: { editable: false, nullable: false },
Name: { validation: { required: true } },
Status: { validation: { required: true } },
IsActive: { type: "boolean" }
}
}
}
});
$("#grid").kendoGrid({
dataSource: dataSource,
pageable: true,
toolbar: ["create"],
scrollable: false,
sortable: true,
groupable: true,
columns: [
{ field: "Name", title: "Project Name", width: "170px" },
{ field: "Status", title: "Status", width: "110px" },
{ field: "IsActive", title: "Active", width: "50px" },
{ command: ["edit", "delete", "Setting","Task"], title: " ", width: "150px" }
],
editable: "popup"
});
});
</script>
控制器代码:
public IQueryable<ProjectsDM> GetProject()
{
return db.Project;
}
[HttpPut]
public IHttpActionResult PutProjectsDM(int id, ProjectsDM projectsdm)
{
if (!ModelState.IsValid)
{
return BadRequest(ModelState);
}
if (id != projectsdm.ProjectId)
{
return BadRequest();
}
db.Entry(projectsdm).State = EntityState.Modified;
try
{
projectsdm.ModifiedBy = "adnan";
projectsdm.ModifiedDate = DateTime.Now;
db.SaveChanges();
}
catch (DbUpdateConcurrencyException)
{
if (!ProjectsDMExists(id))
{
return NotFound();
}
else
{
throw;
}
}
return StatusCode(HttpStatusCode.NoContent);
}
获取方法/操作在kendo网格中正常工作但我面临问题 使用put方法/动作(当我们尝试编辑网格中的某些记录时)....
firebug .net选项卡中的错误
响应:&#34;消息&#34;:&#34;未找到与之匹配的HTTP资源 请求URI&#39; http://localhost:1799/api/project ?/放回调= jQuery191012879030621062526_1433486934717&安培;模型= [{\&#34;专案编号\&#34;:2%2C \&#34;客户端Id \&#34;:1%2C \&#34; FirmId \&#34;:1%2C \&#34;状态\&#34;:\&#34;开始\&#34;%2C \&#34;名称\&#34 ;:\&#34; Flexi77 \&#34;%2C \&#34; IsActive \&#34;:真%2C \&#34; CreatedDate \&#34;:\&#34; 2015-06 -03T00 :00:00 \&#34;%2C \&#34; ModifiedDate \&#34;:\&#34; 2015-06-03T00:00:00 \&#34;%2C \&#34; CreatedBy \&#34;:\&#34;阿德南\&#34;%2C \&#34; ModifiedBy \&#34;:\&#34;阿德南 \&#34;}]&安培; _ = 1433486934719&#39;&#34 ;, 的&GT; &#34; MessageDetail&#34;:&#34;未在控制器上找到任何操作&#39;项目&#39; 符合名称&#39; put&#39;。&#34;
json:&#34;未在控制器上找到任何操作&#39;项目&#39;匹配 名称&#39; put&#39;。&#34;
是否有路线错误或愚蠢的错误,请帮忙.... 任何类型的提示/帮助非常感谢.... 谢谢你的时间
答案 0 :(得分:2)
问题是您的Web API操作需要两个参数:
int id
,请求网址中缺少ProjectsDM projectsdm
,可以从有效负载(请求正文)中检索您需要使用包含缺少id参数的URL。在路线模板中,它是可选的,但是,如果它缺失,作为需要它的动作,动作选择器不能选择动作。
因此,请将id
添加到网址,或从操作中删除id
参数。