1
votes

I'm trying to implement an Odata endpoint using WebApi 2.2, and EF 6.1.2 with a Code First model. I followed the tutorial found here :

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

The GET verb works great as well as the PUT. However, a DELETE request returns a 404 error. Any suggestions on how I can debug this problem?

Here is a sampling of entities, WebApiConfig, and 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
How are you testing? - Carlos Rodriguez
Fiddler. Here is the Uri I'm using that is failing: DELETE localhost:52832/odata/BoltLoads(3) - Greg Rotman

1 Answers

0
votes

I figured out the problem. The Web Site was set to run in Classic mode. Once I changed it to Integrated the DELETE function executed correctly.