云主机测评网云主机测评网云主机测评网

云主机测评网
www.yunzhuji.net

aspnet网站后台源码_网站后台管理

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);
打赏
版权声明:主机测评不销售、不代购、不提供任何支持,仅分享信息/测评(有时效性),自行辨别,请遵纪守法文明上网。
文章名称:《aspnet网站后台源码_网站后台管理》
文章链接:https://www.yunzhuji.net/jishujiaocheng/71150.html

评论

  • 验证码