同步
using System;
using System.Collections.Generic;using System.Data;
using System.Data.Entity;
using System.Data.Entity.Infrastructure;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using System.Web.Http.Description;
using SavMES.Models;
using WebApiFramework.Models;
namespace WebApiFramework.Controllers
{
[Authorize]
[RoutePrefix("api/LieBies")]
public class LieBiesController : ApiController
{
private ApplicationDbContext db = new ApplicationDbContext();
public IQueryable<LieBie> GetLieBies(LieBie model)
{
return db.LieBies;
}
///<summary>
///获取类别
///</summary>
// GET: api/LieBies
[Route("GetLieBies")]
[AllowAnonymous]
[ResponseType(typeof(LieBie))]
public IHttpActionResult GetLieBie(LieBie model)
{
//LieBie lieBie = db.LieBies.Find(model.Id);
//if (lieBie == null)
//{
// return NotFound();
//}
var arr = db.LieBies;
//return Json(new { result = true, errors = "", data = arr, status = 200 });
return Ok(arr);
}
// PUT: api/LieBies/5
[ResponseType(typeof(void))]
public IHttpActionResult PutLieBie(int id, LieBie lieBie)
{
if (!ModelState.IsValid)
{
return BadRequest(ModelState);
}
if (id != lieBie.Id)
{
return BadRequest();
}
db.Entry(lieBie).State = EntityState.Modified;
try
{
db.SaveChanges();
}
catch (DbUpdateConcurrencyException)
{
if (!LieBieExists(id))
{
return NotFound();
}
else
{
throw;
}
}
return StatusCode(HttpStatusCode.NoContent);
}
// POST: api/LieBies
[ResponseType(typeof(LieBie))]
public IHttpActionResult PostLieBie(LieBie lieBie)
{
if (!ModelState.IsValid)
{
return BadRequest(ModelState);
}
db.LieBies.Add(lieBie);
db.SaveChanges();
return CreatedAtRoute("DefaultApi", new { id = lieBie.Id }, lieBie);
}
// DELETE: api/LieBies/5
[ResponseType(typeof(LieBie))]
public IHttpActionResult DeleteLieBie(int id)
{
LieBie lieBie = db.LieBies.Find(id);
if (lieBie == null)
{
return NotFound();
}
db.LieBies.Remove(lieBie);
db.SaveChanges();
return Json(new { result = false, errors = lieBie, data = "", status = 400 });
//return Ok(lieBie);
}
protected override void Dispose(bool disposing)
{
if (disposing)
{
db.Dispose();
}
base.Dispose(disposing);
}
private bool LieBieExists(int id)
{
return db.LieBies.Count(e => e.Id == id) > 0;
}
}
}