ASP.NET 网站后台管理系统的部分源码解析及功能介绍
在ASP.NET中,网站后台管理系统一般包含以下部份:用户管理、内容管理、系统设置等,这种部份的源码可能如下:
(图片来源网路,侵删)
1、用户管理
用户管理部份主要负责用户的增删改查,包括管理员和普通用户。
public class UserController : Controller{ private readonly UserManager _userManager; public UserController(UserManager userManager) { _userManager = userManager; } // GET: Admin/Users public async Task Index() { return View(await _userManager.Users.ToListAsync()); } // GET: Admin/Users/Create public IActionResult Create() { return View(); } // POST: Admin/Users/Create [HttpPost] [ValidateAntiForgeryToken] public async Task Create([Bind("Id,UserName,NormalizedUserName,Email,EmailConfirmed,PasswordHash,SecurityStamp,ConcurrencyStamp,PhoneNumber,PhoneNumberConfirmed,TwoFactorEnabled,LockoutEnd,LockoutEnabled,AccessFailedCount")] ApplicationUser user) { if (ModelState.IsValid) { var result = await _userManager.CreateAsync(user, "YourPasswordHere"); if (result.Succeeded) { return RedirectToAction(nameof(Index)); } AddErrors(result); } return View(user); } // GET: Admin/Users/Edit/5 public async Task Edit(string id) { if (id == null) { return NotFound(); } var user = await _userManager.FindByIdAsync(id); if (user == null) { return NotFound(); } return View(user); } // POST: Admin/Users/Edit/5 [HttpPost] [ValidateAntiForgeryToken] public async Task Edit(string id, [Bind("Id,UserName,NormalizedUserName,Email,EmailConfirmed,PasswordHash,SecurityStamp,ConcurrencyStamp,PhoneNumber,PhoneNumberConfirmed,TwoFactorEnabled,LockoutEnd,LockoutEnabled,AccessFailedCount")] ApplicationUser user) { if (id != user.Id) { return NotFound(); } if (ModelState.IsValid) { try { var result = await _userManager.UpdateAsync(user); if (result.Succeeded) { return RedirectToAction(nameof(Index)); } AddErrors(result); } catch (DbUpdateConcurrencyException) { if (!await UserExists(user.Id)) { return NotFound(); } else { throw; } } return View(user); } return View(user); } // GET: Admin/Users/Delete/5 public async Task Delete(string id) { if (id == null) { return NotFound(); } var user = await _userManager.FindByIdAsync(id); if (user == null) { return NotFound(); } return View(user); } // POST: Admin/Users/Delete/5 [HttpPost, ActionName("Delete")] [ValidateAntiForgeryToken] public async Task DeleteConfirmed(string id) { var user = await _userManager.FindByIdAsync(id); if (user != null) { await _userManager.DeleteAsync(user); } return RedirectToAction(nameof(Index)); } private bool UserExists(string id) { return _userManager.Users.Any(e => e.Id == id); }}
2、内容管理
内容管理部份主要负责文章、产品等内容的增删改查。
public class ContentController : Controller{ private readonly ContentManager _contentManager; public ContentController(ContentManager contentManager) { _contentManager = contentManager; } // GET: Admin/Contents public async Task Index() { return View(await _contentManager.Contents.ToListAsync()); } // GET: Admin/Contents/Create public IActionResult Create() { return View(); } // POST: Admin/Contents/Create [HttpPost] [ValidateAntiForgeryToken] public async Task Create([Bind("Id,Title,Content,Author,PublishDate")] Content content) { if (ModelState.IsValid) { await _contentManager.CreateAsync(content); return RedirectToAction(nameof(Index)); } return View(content);