网站首页 网站源码
using System.Net;
using Dpz.Core.EnumLibrary;
using Dpz.Core.Hangfire;
using Dpz.Core.Web.Models;
using Hangfire;
using Microsoft.AspNetCore.Mvc.ModelBinding;
namespace Dpz.Core.Web.Controllers;
public class CommentController(ICommentService commentService) : Controller
{
[HttpGet("Comment/{node}/{relation}")]
public async Task<IActionResult> Index(CommentNode node, string relation, int pageIndex = 1, int pageSize = 5)
{
relation = WebUtility.UrlDecode(relation);
ViewData["node"] = node;
ViewData["relation"] = relation;
var list = await commentService.GetCommentsAsync(node, relation, pageIndex, pageSize);
var count = await commentService.GetCommentCountAsync(node, relation);
var model = new CommentPage { Page = list, Count = count };
Response.Headers.Append("CommentCount", count.ToString());
return PartialView(model);
}
[HttpGet("Comment/Page/{node}/{relation}")]
public async Task<IActionResult> Page(CommentNode node, string relation, int pageIndex = 1, int pageSize = 5)
{
relation = WebUtility.UrlDecode(relation);
ViewData["node"] = node;
ViewData["relation"] = relation;
var list = await commentService.GetCommentsAsync(node, relation, pageIndex, pageSize);
var count = await commentService.GetCommentCountAsync(node, relation);
var model = new CommentPage { Page = list, Count = count };
Response.Headers.Append("CommentCount", count.ToString());
return PartialView("_CommentListPartial", model);
}
[HttpPost, ValidateAntiForgeryToken]
public async Task<IActionResult> Publish(VmPublishComment comment)
{
if (!ModelState.IsValid)
{
var errors = ModelState
.SelectMany(x => x.Value?.Errors ?? new ModelErrorCollection())
.Select(x => x.ErrorMessage)
.Where(x => !string.IsNullOrEmpty(x))
.ToArray();
return Json(new ResultInfo(string.Join("\n", errors)));
}
await commentService.PublishCommentAsync(comment);
var list = await commentService.GetCommentsAsync(comment.Node, comment.Relation, 1, 5);
var count = await commentService.GetCommentCountAsync(comment.Node, comment.Relation);
var model = new CommentPage { Page = list, Count = count };
ViewData["node"] = comment.Node;
ViewData["relation"] = comment.Relation;
SetCookie(comment.NickName, comment.Email, comment.Site);
BackgroundJob.Enqueue<EmailSenderActivator>(x => x.SendMasterAsync(comment));
if (!string.IsNullOrEmpty(comment.ReplyId))
{
BackgroundJob.Enqueue<EmailSenderActivator>(x => x.SendCommenterAsync(comment));
}
ViewData["node"] = comment.Node;
ViewData["relation"] = comment.Relation;
Response.Headers.Append("CommentCount", count.ToString());
return PartialView("_CommentListPartial", model);
}
[NonAction]
private void SetCookie(string nickname, string email, string? site)
{
var cookieNickname = Request.Cookies[nameof(nickname)];
if (string.IsNullOrEmpty(cookieNickname) || WebUtility.UrlDecode(cookieNickname) != nickname)
{
Response.Cookies.Append(nameof(nickname), WebUtility.UrlEncode(nickname),
new CookieOptions
{ HttpOnly = true, Secure = true, IsEssential = true, Expires = DateTimeOffset.Now.AddYears(1) });
}
var cookieEmail = Request.Cookies[nameof(email)];
if (string.IsNullOrEmpty(cookieEmail) || WebUtility.UrlDecode(cookieEmail) != email)
{
Response.Cookies.Append(nameof(email), WebUtility.UrlEncode(email),
new CookieOptions
{ HttpOnly = true, Secure = true, IsEssential = true, Expires = DateTimeOffset.Now.AddYears(1) });
}
var cookieSite = Request.Cookies[nameof(site)];
if (!string.IsNullOrEmpty(site) &&
(string.IsNullOrEmpty(cookieSite) || WebUtility.UrlDecode(cookieSite) != site))
{
Response.Cookies.Append(nameof(site), WebUtility.UrlEncode(site),
new CookieOptions
{ HttpOnly = true, Secure = true, IsEssential = true, Expires = DateTimeOffset.Now.AddYears(1) });
}
}
[HttpPost, ValidateAntiForgeryToken, CheckAuthorize]
public async Task<IActionResult> Send(MembleComment comment)
{
if (!ModelState.IsValid)
{
var errors = ModelState
.SelectMany(x => x.Value?.Errors ?? new ModelErrorCollection())
.Select(x => x.ErrorMessage)
.Where(x => !string.IsNullOrEmpty(x))
.ToArray();
return Json(new ResultInfo(string.Join("\n", errors)));
}
var user = User.GetIdentity();
comment.User = user;
await commentService.PublishCommentAsync(comment);
var list = await commentService.GetCommentsAsync(comment.Node, comment.Relation, 1, 5);
var count = await commentService.GetCommentCountAsync(comment.Node, comment.Relation);
var model = new CommentPage { Page = list, Count = count };
Response.Headers.Append("CommentCount", count.ToString());
if (!string.IsNullOrEmpty(comment.ReplyId))
{
BackgroundJob.Enqueue<EmailSenderActivator>(x => x.SendCommenterAsync(comment));
}
ViewData["node"] = comment.Node;
ViewData["relation"] = comment.Relation;
return PartialView("_CommentListPartial", model);
}
}
上述代码是一个 ASP.NET Core MVC 控制器,名为 CommentController
,用于处理与评论相关的操作。以下是代码的主要功能和结构的详细解释:
CommentController
依赖于一个 ICommentService
服务,该服务负责处理与评论相关的业务逻辑。控制器提供了多个操作(Action),用于获取评论、发布评论和处理用户的评论请求。
Index
方法Comment/{node}/{relation}
relation
参数。commentService
获取评论列表和评论总数。CommentPage
模型中,并返回一个部分视图(Partial View)。Page
方法Comment/Page/{node}/{relation}
Index
方法,但返回的部分视图是 _CommentListPartial
。Publish
方法commentService
发布评论。SetCookie
方法Send
方法Publish
方法。CommentPage
用于封装评论列表和总数。ModelState
验证输入,并返回错误信息。ValidateAntiForgeryToken
特性来防止跨站请求伪造(CSRF)攻击。CommentController
提供了一个完整的评论管理功能,包括获取评论、发布评论、处理用户输入和发送通知。它通过依赖注入的方式使用 ICommentService
来处理业务逻辑,并通过部分视图更新用户界面。