【问题标题】:WebApi OData only able to POST and GET entitiesWebApi OData 只能 POST 和 GET 实体
【发布时间】:2014-05-03 00:18:31
【问题描述】:

我使用 WebApi OData 创建了一个简单的 CRUD 应用程序。我一直在使用 Fiddler 2 的 Composer 选项卡来测试服务,无论我做什么,我都会不断从本地 IIS 服务器获得 HTTP 404 响应。如前所述,POST 请求有效:

http://mymachine/Service.Facade.Reports/odata/Reports

用户代理:提琴手

内容类型:application/json

主机:本地主机

内容长度:1373

{ "名称":"报表...","组":"Hhowd444","ReportLayoutID":8270,"DatabaseInstanceID":1042 }

所以我尝试了:

但是,将 URL 更改为:> 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 }
    );
}

来自控制器的 Post 和 Put:

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);
}

【问题讨论】:

    标签: c# asp.net-web-api


    【解决方案1】:

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

    解决方法:

    1. IIS 管理器
    2. 处理程序映射
    3. 找到 ExpresslessUrlHandler...条目 x 3
    4. 右键单击,编辑
    5. 选择请求限制
    6. 点击动词标签
    7. 要么添加额外的动词,要么只选择所有动词

    经过一些痛苦和磨难,现在一切正常!

    【讨论】:

      猜你喜欢
      • 2014-12-11
      • 2013-09-02
      • 1970-01-01
      • 1970-01-01
      • 2013-03-23
      • 2014-09-07
      • 1970-01-01
      • 2013-08-05
      • 2018-06-29
      相关资源
      最近更新 更多