类图:

CategoryController.cs
using System.Data.Entity;
using System.Data.Entity.Infrastructure;
using System.Linq;
using System.Net;
using System.Web.Http;
using System.Web.Http.Description;
using Layui.Models;
namespace Layui.WebApiControllers
{
public class CategoryController : ApiController
{
private readonly Entities db = new Entities();
public IQueryable<Category> GetCategorys()
{
return db.Categorys;
}
[ResponseType(typeof (Category))]
public IHttpActionResult GetCategory(int id)
{
Category category = db.Categorys.Find(id);
if (category == null)
{
return NotFound();
}
return Ok(category);
}
public IHttpActionResult PutCategory(int id, Category category)
{
if (!ModelState.IsValid)
{
return BadRequest(ModelState);
}
if (id != category.Id)
{
return BadRequest();
}
db.Entry(category).State = EntityState.Modified;
try
{
db.SaveChanges();
}
catch (DbUpdateConcurrencyException)
{
if (!CategoryExists(id))
{
return NotFound();
}
throw;
}
return StatusCode(HttpStatusCode.NoContent);
}
[ResponseType(typeof (Category))]
public IHttpActionResult PostCategory(Category category)
{
if (!ModelState.IsValid)
{
return BadRequest(ModelState);
}
db.Categorys.Add(category);
db.SaveChanges();
return CreatedAtRoute("DefaultApi", new {id = category.Id}, category);
}
[ResponseType(typeof (Category))]
public IHttpActionResult DeleteCategory(int id)
{
Category category = db.Categorys.Find(id);
if (category == null)
{
return NotFound();
}
db.Categorys.Remove(category);
db.SaveChanges();
return Ok(category);
}
protected override void Dispose(bool disposing)
{
if (disposing)
{
db.Dispose();
}
base.Dispose(disposing);
}
private bool CategoryExists(int id)
{
return db.Categorys.Count(e => e.Id == id) > 0;
}
}
}
UserController.cs
using System.Collections.Generic
using System.Linq
using System.Web.Http
using Layui.Models
using Layui.ViewModels
using Microsoft.AspNet.Identity
using Microsoft.AspNet.Identity.EntityFramework
namespace Layui.WebApiControllers
{
public class UserController : ApiController
{
private readonly ApplicationDbContext db = new ApplicationDbContext()
public UserController() : this(new UserManager<ApplicationUser>(new UserStore<ApplicationUser>(new ApplicationDbContext())))
{
}
public UserController(UserManager<ApplicationUser> userManager)
{
UserManager = userManager
}
public UserManager<ApplicationUser> UserManager { get
// GET api/user
public IEnumerable<UserViewModel> Get()
{
IQueryable<UserViewModel> users = from u in db.Users
select new UserViewModel
{
Id = u.Id,
UserName = u.UserName,
Alias = u.Alias,
Comments = u.Comments,
Articles = u.Articles,
Email = u.Email,
Intro = u.Intro,
IP = u.IP,
Meta = u.Meta,
Pages = u.Pages,
PostTime = u.PostTime,
Template = u.Template,
Uploads = u.Uploads
}
return users
}
// GET api/user/5
public UserViewModel Get(string userId)
{
ApplicationUser u = UserManager.FindById(userId)
var user = new UserViewModel
{
Id = u.Id,
UserName = u.UserName,
Alias = u.Alias,
Comments = u.Comments,
Articles = u.Articles,
Email = u.Email,
Intro = u.Intro,
IP = u.IP,
Meta = u.Meta,
Pages = u.Pages,
PostTime = u.PostTime,
Template = u.Template,
Uploads = u.Uploads,
RoleId = string.Empty
}
List<IdentityUserRole> r = u.Roles.ToList()
if (r.Count() > 0)
{
user.RoleId = r[0].RoleId
}
return user
}
// POST api/user
public void Post([FromBody] UserViewModel user)
{
var u = new ApplicationUser {UserName = user.UserName}
IdentityResult result = UserManager.Create(u, "asdkjfi2n@sdklsadiopas$ed8asdkAfq2kf")
UserManager.Update(u)
}
// PUT api/user/5
public IHttpActionResult Put(string userId, [FromBody] UserViewModel user)
{
ApplicationUser u = UserManager.FindById(user.Id)
u.UserName = user.UserName
u.Alias = user.Alias
u.Comments = user.Comments
u.Articles = user.Articles
u.Email = user.Email
u.Intro = user.Intro
u.IP = user.IP
u.Meta = user.Meta
u.Pages = user.Pages
u.PostTime = user.PostTime
u.Template = user.Template
u.Uploads = user.Uploads
if (!string.IsNullOrEmpty(user.RoleId))
{
UserManager.AddToRole(user.Id, "超级管理员")
}
IdentityResult result = UserManager.Update(u)
return Ok(result)
}
// DELETE api/user/5
public void Delete(string userId)
{
ApplicationUser user = UserManager.FindById(userId)
}
protected override void Dispose(bool disposing)
{
if (disposing && UserManager != null)
{
UserManager.Dispose()
db.Dispose()
UserManager = null
}
base.Dispose(disposing)
}
}
}
运行结果如图:


