在ASP.NET中,网站后台管理系统通常包含以下部分:用户管理、内容管理、系统设置等,这些部分的源码可能如下:
(图片来源网络,侵删)
1、用户管理
用户管理部分主要负责用户的增删改查,包括管理员和普通用户。
public class UserController : Controller { private readonly UserManager<ApplicationUser> _userManager; public UserController(UserManager<ApplicationUser> userManager) { _userManager = userManager; } // GET: Admin/Users public async Task<IActionResult> 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<IActionResult> 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<IActionResult> 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<IActionResult> 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<IActionResult> 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<IActionResult> 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<IActionResult> 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<IActionResult> Create([Bind("Id,Title,Content,Author,PublishDate")] Content content) { if (ModelState.IsValid) { await _contentManager.CreateAsync(content); return RedirectToAction(nameof(Index)); } return View(content);
原创文章,作者:未希,如若转载,请注明出处:https://www.kdun.com/ask/684596.html
本网站发布或转载的文章及图片均来自网络,其原创性以及文中表达的观点和判断不代表本网站。如有问题,请联系客服处理。
发表回复