我已经使用WebApi OData创建了一个简单的CRUD应用程序。我一直在使用Fiddler 2的Composer选项卡来测试服务,无论我做什么,我都会从本地IIS服务器获取HTTP 404响应。如前所述一个POST要求工作:WebApi OData只能够POST和GET实体
http://mymachine/Service.Facade.Reports/odata/Reports
的User-Agent:提琴手
内容类型:应用程序/ JSON
主机:本地主机
的Content-Length :1373
{ “名称”: “报告......”, “集团”: “Hhowd444”, “ReportLayoutID”:8270, “DatabaseInstanceID”:1042 }
所以,我想:
然而,网址更改为:>
http://mymachine/Service.Facade.Reports/odata/Reports(8270)
和 执行PUT不起作用
的PUT导致404错误。我尝试调试和跟踪服务,导致无处。
在WebApiConfig类:
public static void Register(HttpConfiguration config)
{
ODataModelBuilder builder = new ODataConventionModelBuilder();
builder.EntitySet<ReportLayout>("Reports");
builder.EntitySet<ReportLayoutData>("ReportLayoutData");
builder.EntitySet<DatabaseInstance>("DataSources");
builder.EntitySet<DataSourceObject>("DataSourceObject");
config.Routes.MapODataRoute("odata", "odata", builder.GetEdmModel());
config.MapHttpAttributeRoutes();
config.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}/{key}",
defaults: new { key = RouteParameter.Optional }
);
}
岗位和从控制器认沽:
public async Task<IHttpActionResult> Post(ReportLayout reportlayout)
{
if (!ModelState.IsValid)
{
return BadRequest(ModelState);
}
db.Reports.Add(reportlayout);
db.ReportsData.AddRange(reportlayout.LayoutData);
await db.SaveChangesAsync();
return Created(reportlayout);
}
public async Task<IHttpActionResult> Put([FromODataUri] int key, ReportLayout reportlayout)
{
if (!ModelState.IsValid)
{
return BadRequest(ModelState);
}
if (key != reportlayout.ReportLayoutID)
{
return BadRequest();
}
db.Entry(reportlayout).State = EntityState.Modified;
try
{
await db.SaveChangesAsync();
}
catch (DbUpdateConcurrencyException)
{
if (!ReportLayoutExists(key))
{
return NotFound();
}
else
{
throw;
}
}
return Updated(reportlayout);
}