【问题标题】:Render action performance渲染动作性能
【发布时间】:2012-01-31 21:01:45
【问题描述】:

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

要显示我想使用的所有数据,如下所示:

    [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调用造成的性能产生不良影响。

【问题讨论】:

    标签: asp.net-mvc asp.net-mvc-3 renderaction


    【解决方案1】:

    很遗憾,这会导致性能不佳。 RenderAction 以其惊人的速度而闻名。

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

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

    【讨论】:

      猜你喜欢
      • 2012-12-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-01-09
      • 2011-08-26
      • 2013-11-22
      • 2012-10-06
      • 2011-11-06
      相关资源
      最近更新 更多