简单的MVC评论审核

led*_*per 5 c# asp.net-mvc optimization entity-framework

我有一种简单(也可能是粗略)的方式来审核我正在构建的博客上的评论。这是一个学习/有趣的项目,所以我正在尽我所能,以更熟悉一些不同的技术。我想知道我的逻辑是否有任何漏洞,或者我正在做的事情是否有更好的实现。我将允许在该网站上发表匿名评论,但我想对我认为不合适的任何内容进行审核。这是我如何做到的:

我的模型使用 EF 代码优先方法:

public class Comment
{
    public int Id { get; set; }
    public bool Moderated { get; set; }
    public string DisplayName { get; set; }
    public string Email { get; set; }
    public DateTime DateCreated { get; set; }
    public string Content { get; set; }
    public int PostId { get; set; }
    public Post Post { get; set; }
}
Run Code Online (Sandbox Code Playgroud)

标准的东西在这里。然后我创建了一个 ViewModel 来显示博客文章的详细信息以及页面上与其相关的所有评论,如下所示:

public class PostCommentViewModel
{
    public Post Post { get; set; }
    public List<Comment> Comment { get; set; }

    public PostCommentViewModel(int postId)
    {
        var db = new BlogContext();

        Post = db.Posts.First(x => x.Id == postId);
        var query = from x in db.Comments where x.PostId == postId && x.Moderated == true select x;

        Comment = query.ToList();
    }
}
Run Code Online (Sandbox Code Playgroud)

对于评论,这只是抓取与 PostId 相关且经过审核的评论(即我已经能够查看它们)

对于显示这个的视图,我只使用了一个基本的脚手架模板:

public ActionResult Details(int id = 0)
    {
        var viewModel = new PostCommentViewModel(id);
        return View(viewModel);
    }
Run Code Online (Sandbox Code Playgroud)

cshtml:

@model CodeFirstBlog.ViewModels.PostCommentViewModel


<fieldset>
<legend>PostCommentViewModel</legend>
@Html.DisplayFor(x => x.Post.Title)
<br />
@Html.DisplayFor(x => x.Post.Content)
<br />
@Html.DisplayFor(x => x.Post.CreatedDate)
<hr />    
@foreach(var comment in Model.Comment)
{
    @Html.DisplayFor(x => comment.Content)
    <br />
    @Html.DisplayFor(x => comment.DateCreated)
    <br />
    @Html.DisplayFor(x => comment.DisplayName)
    <br />
    @Html.DisplayFor(x => comment.Email)
    <br />
    <hr />
}

</fieldset>
@Html.ActionLink("Add Comment", "AddComment", new { id = Model.Post.Id} )
Run Code Online (Sandbox Code Playgroud)

这是控制器中的 AddComment

public ActionResult AddComment(int id = 0)
    {
        return View();
    }

    [HttpPost]
    public ActionResult AddComment(Comment comment, int id)
    {
        if (ModelState.IsValid)
        {
            comment.PostId = id;
            db.Comments.Add(comment);
            db.SaveChanges();
            return RedirectToAction("Details", "Blog", new { id = id });
        }
        return RedirectToAction("Details", "Blog", new { id = id });
    }
Run Code Online (Sandbox Code Playgroud)

因此,当我添加评论时,Moderated 默认为 false,因此评论不会立即显示。现在,如果管理员登录,他可以转到 ViewModeration 视图,该视图仅返回所有等待批准的评论列表:

public ActionResult ViewModeration()
    {
        var comments = from x in db.Comments where x.Moderated == false select x;
        return View(comments);
    }
Run Code Online (Sandbox Code Playgroud)

如果他单击批准按钮,它会在控制器中执行此操作:

 public ActionResult ApproveComment(int id)
    {
        Comment c = (from x in db.Comments
                     where x.Id == id
                     select x).First();
        c.Moderated = true;
        db.SaveChanges();
        return RedirectToAction("ViewModeration");
    }
Run Code Online (Sandbox Code Playgroud)

我真正想知道的是:

  1. 此实现中是否存在任何漏洞,例如知情用户是否可以覆盖回发中的审核值?
  2. 是否有更简单或更优雅的解决方案可供遵循?同样,我不想使用任何预先构建的东西。这个项目的重点是学习东西。

All*_*lov 4

该模型到目前为止似乎还没有进展,所以我会这样回答这个问题:

此实现中是否存在任何漏洞,例如知情用户是否可以覆盖回发中的调节值?

是的。我没有根据您发送的内容创建任何表单代码,但我假设您正在通过发布值创建评论并直接保存到数据库。这可能很糟糕。您最好只从用户那里获取所需的值,并将其余值填充到控制器中:

public ActionResult AddComment(Comment comment, int id)
{
    if (ModelState.IsValid)
    {
        // NEW
        comment.Moderated = false;

        comment.PostId = id;
        db.Comments.Add(comment);
        db.SaveChanges();
        return RedirectToAction("Details", "Blog", new { id = id });
    }
    return RedirectToAction("Details", "Blog", new { id = id });
}
Run Code Online (Sandbox Code Playgroud)

是否有更简单或更优雅的解决方案可供遵循?再说一次,我不想使用任何预先构建的东西。这个项目的目的是学习东西。

如前所述,到目前为止,这看起来不错。但是,出于可重用性和测试目的,我可能会在不同的类中获取数据库操作,例如服务或存储库。

所以,代码看起来像这样:

public ActionResult AddComment(Comment comment, int id)
{
    if (ModelState.IsValid)
    {
        CommentService.Save(comment);
        return RedirectToAction("Details", "Blog", new { id = id });
    }
    return RedirectToAction("Details", "Blog", new { id = id });
}
Run Code Online (Sandbox Code Playgroud)

这并不是一个很大的变化,但从某种意义上来说它是巨大的,因为如果您想重用此代码,它将为您提供更大的灵活性。

至于您的 PostCommentViewModel 类,我不会在 ViewModel 中执行任何操作,特别是在构造函数中。您应该使用 ViewModel 的方式是将数据绑定到它上面,而不是让 ViewModel 来完成这项工作。无论如何,您都可以获取数据,ViewModel 仅代表需要显示的结构。因此,将代码从那里取出,并将其放入服务中(即:CommentService)。