WebAPI Odata删除操作

时间:2015-02-10 17:53:20

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

我正在尝试使用WebApi 2.2实现Odata端点,并使用Code First模型实现EF 6.1.2。我按照这里的教程进行了操作:

http://www.asp.net/web-api/overview/odata-support-in-aspnet-web-api/odata-v3/creating-an-odata-endpoint

GET动词和PUT一样好用。但是,DELETE请求返回404错误。关于如何调试此问题的任何建议?

以下是实体,WebApiConfig和Controller的示例。

public partial class BoltLoad
{
    public int Id { get; set; }

    [StringLength(25)]
    public string LoadNumber { get; set; }

    public string BoltUserId { get; set; }

    [Column(TypeName = "datetime2")]
    public DateTime CompletedDate { get; set; }

    [StringLength(50)]
    public string PickupAttention { get; set; }

    [StringLength(50)]
    public string PickupLine1 { get; set; }

    [StringLength(50)]
    public string PickupCity { get; set; }

    [StringLength(50)]
    public string PickupStateProvince { get; set; }

    [StringLength(50)]
    public string DropAttention { get; set; }

    [StringLength(50)]
    public string DropLine1 { get; set; }

    [StringLength(50)]
    public string DropCity { get; set; }

    [StringLength(50)]
    public string DropStateProvince { get; set; }

    public Guid GlobalId { get; set; }

    [Column(TypeName = "timestamp")]
    [MaxLength(8)]
    [Timestamp]
    public byte[] Version { get; set; }

    [Column(TypeName = "datetime2")]
    public DateTime CreatedOn { get; set; }

    [StringLength(256)]
    public string CreatedBy { get; set; }

    [Column(TypeName = "datetime2")]
    public DateTime ModifiedOn { get; set; }

    [StringLength(256)]
    public string ModifiedBy { get; set; }
}

public static class WebApiConfig
{
    public static void Register(HttpConfiguration config)
    {
        ODataConventionModelBuilder builder = new ODataConventionModelBuilder();
        builder.EntitySet<BoltLoad>("BoltLoads");
        var t = config.Routes.MapODataServiceRoute("odata", "odata", builder.GetEdmModel());


    }
}


public class BoltLoadsController : ODataController
{
    private AnyWare db = new AnyWare();

    // GET: odata/BoltLoads
    [EnableQuery]
    public IQueryable<BoltLoad> GetBoltLoads()
    {
        return db.BoltLoads;
    }

    // GET: odata/BoltLoads(5)
    [EnableQuery]
    public SingleResult<BoltLoad> GetBoltLoad([FromODataUri] int key)
    {
        return SingleResult.Create(db.BoltLoads.Where(boltLoad => boltLoad.Id == key));
    }

    // PUT: odata/BoltLoads(5)
    public IHttpActionResult Put([FromODataUri] int key, Delta<BoltLoad> patch)
    {
        Validate(patch.GetEntity());

        if (!ModelState.IsValid)
        {
            return BadRequest(ModelState);
        }

        BoltLoad boltLoad = db.BoltLoads.Find(key);
        if (boltLoad == null)
        {
            return NotFound();
        }

        patch.Put(boltLoad);

        try
        {
            db.SaveChanges();
        }
        catch (DbUpdateConcurrencyException)
        {
            if (!BoltLoadExists(key))
            {
                return NotFound();
            }
            else
            {
                throw;
            }
        }

        return Updated(boltLoad);
    }

    // POST: odata/BoltLoads
    public IHttpActionResult Post(BoltLoad boltLoad)
    {
        if (!ModelState.IsValid)
        {
            return BadRequest(ModelState);
        }

        db.BoltLoads.Add(boltLoad);
        db.SaveChanges();

        return Created(boltLoad);
    }

    // PATCH: odata/BoltLoads(5)
    [AcceptVerbs("PATCH", "MERGE")]
    public IHttpActionResult Patch([FromODataUri] int key, Delta<BoltLoad> patch)
    {
        Validate(patch.GetEntity());

        if (!ModelState.IsValid)
        {
            return BadRequest(ModelState);
        }

        BoltLoad boltLoad = db.BoltLoads.Find(key);
        if (boltLoad == null)
        {
            return NotFound();
        }

        patch.Patch(boltLoad);

        try
        {
            db.SaveChanges();
        }
        catch (DbUpdateConcurrencyException)
        {
            if (!BoltLoadExists(key))
            {
                return NotFound();
            }
            else
            {
                throw;
            }
        }

        return Updated(boltLoad);
    }

    // DELETE: odata/BoltLoads(5)

    public IHttpActionResult Delete([FromODataUri] int key)
    {
        BoltLoad boltLoad = db.BoltLoads.Find(key);
        if (boltLoad == null)
        {
            return NotFound();
        }

        db.BoltLoads.Remove(boltLoad);
        db.SaveChanges();

        return StatusCode(HttpStatusCode.NoContent);
    }

    protected override void Dispose(bool disposing)
    {
        if (disposing)
        {
            db.Dispose();
        }
        base.Dispose(disposing);
    }

    private bool BoltLoadExists(int key)
    {
        return db.BoltLoads.Count(e => e.Id == key) > 0;
    }
}

1 个答案:

答案 0 :(得分:0)

我弄明白了这个问题。网站设置为在经典模式下运行。一旦我将其更改为Integrated,DELETE功能就会正确执行。