渲染动作性能

发布于 2025-01-01 07:53:55 字数 1891 浏览 0 评论 0原文

我有显示问题、答案、对答案和问题的评论的视图。

要显示所有数据,我想使用这样的东西:

    [HttpGet, ChildActionOnly]
    public PartialViewResult RenderQuestion(int questionId, int page, int pageSize, string sort)//For question display
    {
        var question = questionsService.GetQuestion(questionId);
        var author = userService.GetUser(question.AuthorId);
        var commentIds = commentService.GetTopCommentIds(questionId, int.MaxValue, CommentType.Question);
        var answerIds = answerService.GetAnswerIdsByQuestion(page, pageSize, questionId, sort);
        var model = new QuestionModel{ Question = question, Author = author, CommentIds = commentIds, AnswerIds = answerIds}
        return PartialView("_Question", model);
    }
    [HttpGet, ChildActionOnly]
    public PartialViewResult RenderAnswer(int answerId)
    {
        var answer = answerService.GetAnswer(answerId);
        var author = userService.GetUser(answer.AuthorId);
        var commentIds = commentService.GetTopCommentIds(answerId, int.MaxValue, CommentType.Answer);
        var model = new AnswerModel { Answer = answer, Author = author, CommentIds = commentIds};
        return PartialView("_Answer");
    }

    [HttpGet, ChildActionOnly]
    public PartialViewResult RenderComment(int commentId, CommentType commentType)
    {
        var comment = commentService.GetComment(commentId, commentType);
        var author = userService.GetUser(comment.AuthorId);
        var model = new CommentModel { Comment = comment, Author = author};
        return PartialView("_Comment");
    }

在我的部分视图中,例如对于问题,我将在循环 Model.AnswerIds 中迭代并调用 @{ Html.RenderAction("RenderAnswer" , new {answerId}) }; 和 Model.CommentIds 并调用 @{ Html.RenderAction("RenderComment", new {commentId}) };

我想要要知道,这是一种很好的视图分解方式吗?这种方式是否会对性能造成不良影响,导致经常调用@Html.RenderAction

I have view that display Question, Answers, Comments to answers and to Question.

To display all data i want to use something like this:

    [HttpGet, ChildActionOnly]
    public PartialViewResult RenderQuestion(int questionId, int page, int pageSize, string sort)//For question display
    {
        var question = questionsService.GetQuestion(questionId);
        var author = userService.GetUser(question.AuthorId);
        var commentIds = commentService.GetTopCommentIds(questionId, int.MaxValue, CommentType.Question);
        var answerIds = answerService.GetAnswerIdsByQuestion(page, pageSize, questionId, sort);
        var model = new QuestionModel{ Question = question, Author = author, CommentIds = commentIds, AnswerIds = answerIds}
        return PartialView("_Question", model);
    }
    [HttpGet, ChildActionOnly]
    public PartialViewResult RenderAnswer(int answerId)
    {
        var answer = answerService.GetAnswer(answerId);
        var author = userService.GetUser(answer.AuthorId);
        var commentIds = commentService.GetTopCommentIds(answerId, int.MaxValue, CommentType.Answer);
        var model = new AnswerModel { Answer = answer, Author = author, CommentIds = commentIds};
        return PartialView("_Answer");
    }

    [HttpGet, ChildActionOnly]
    public PartialViewResult RenderComment(int commentId, CommentType commentType)
    {
        var comment = commentService.GetComment(commentId, commentType);
        var author = userService.GetUser(comment.AuthorId);
        var model = new CommentModel { Comment = comment, Author = author};
        return PartialView("_Comment");
    }

And at my partial views for example for question i will iterate in loop Model.AnswerIds and call @{ Html.RenderAction("RenderAnswer", new {answerId}) }; and Model.CommentIds and call @{ Html.RenderAction("RenderComment", new {commentId}) };

I want to know, is it a good way of view decomposition and will this way have bad influence on performance caused often @Html.RenderAction calls.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

半﹌身腐败 2025-01-08 07:53:55

不幸的是,这会导致性能不佳。 RenderAction 并不以其惊人的速度而闻名。

您还将多次实例化控制器(也可能多次打开数据库)。

我建议您将所有内容放入一个专门的控制器操作中。

Unfortunately, this will cause bad performance. RenderAction is not known for its blazing speed.

You will also instantiate your controller multiple times (possibly opening the database multiple times too).

I recommend you put everything into a single specialized controller action.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文