WebAPI Odata 删除操作
WebAPI Odata Delete Action
我正在尝试使用 WebApi 2.2 和 EF 6.1.2 以及代码优先模型来实现 Odata 端点。我遵循了此处的教程:
GET 动词和 PUT 一样有效。但是,DELETE 请求 returns 出现 404 错误。关于如何调试此问题有什么建议吗?
这是实体、WebApiConfig 和控制器的示例。
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;
}
}
我找到问题了。网站在经典模式下设置为 运行。一旦我将其更改为 Integrated,DELETE 功能就会正确执行。
我正在尝试使用 WebApi 2.2 和 EF 6.1.2 以及代码优先模型来实现 Odata 端点。我遵循了此处的教程:
GET 动词和 PUT 一样有效。但是,DELETE 请求 returns 出现 404 错误。关于如何调试此问题有什么建议吗?
这是实体、WebApiConfig 和控制器的示例。
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;
}
}
我找到问题了。网站在经典模式下设置为 运行。一旦我将其更改为 Integrated,DELETE 功能就会正确执行。