2014-05-02 67 views
0

我已经使用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); 
} 

回答

0

最终的代码不是问题,但我的IIS 7.5服务器的配置。

要解决去:

  1. IIS管理器
  2. 处理程序映射
  3. 找到ExpresslessUrlHandler ...条目×3
  4. 点击右键,编辑
  5. 选择请求限制
  6. 点击动词选项卡
  7. 要么添加额外的动词或者强制t选择所有动词

现在所有的东西都经过一些痛苦和痛苦之后,