如何并行渲染 MVC 操作以减少页面的整体渲染时间?

发布于 2024-09-12 21:00:33 字数 625 浏览 3 评论 0原文

我有一个页面,上面有很多小部件。每个小部件都是一个视图。现在,小部件渲染是在 foreach 循环中完成的,如下所示。

public class WidgetCollection : List<Widget>, IPersonalizable
{
    public void Render(HtmlHelper html)
    {            
        foreach (Widget w in this)
        {

            html.RenderAction("Default", "Widget", new { model = w });
        }
    }

但这意味着我的一些小部件由于 IO 限制而需要 800 毫秒渲染,因此会阻塞其他仅需要 100 毫秒渲染的小部件。因此渲染页面总共花费的时间约为 3 秒。我希望页面在 800 毫秒多一点或尽可能接近的时间内渲染。

我的一个想法是调用 html.Action() 来并行获取每个操作的字符串值,但 MVC 似乎不喜欢并行渲染视图。当我尝试执行此操作时,我总是收到“对象未设置为对象实例”错误。该错误来自 MVC 堆栈深处,因此我认为这只是一个 MVC 错误。

有谁有更好的想法来提高页面渲染速度?

I have a page that has a bunch of widgets on it. Each widget is a view. Right now the widget rendering is done in a foreach loop like so.

public class WidgetCollection : List<Widget>, IPersonalizable
{
    public void Render(HtmlHelper html)
    {            
        foreach (Widget w in this)
        {

            html.RenderAction("Default", "Widget", new { model = w });
        }
    }

But that means that some of my widgets that render in 800ms because they're IO bound are blocking a bunch of other widgets than only take 100ms to render. So in total the time it takes to render the page is about 3 seconds. I want the page to render in just a bit over 800ms or as close as possible to that.

One idea I had was to call html.Action() to get a string value for each action in parallel but MVC doesn't seem to like rendering views in parallel. I always get an "Object not set to the instance of an object" error when I attempt to do it. The error comes from deep in the MVC stack so I think it is just an MVC bug.

Does anyone have a better idea for increasing page rendering speed?

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

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

发布评论

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

评论(2

回忆躺在深渊里 2024-09-19 21:00:33

你可以使用区域并通过ajax“填充”它们吗?

Can you use areas and "fill" them via ajax?

ヤ经典坏疍 2024-09-19 21:00:33

Paul,

您的基本控制器可以继承一个 AsyncController 类。这可能是你最好的选择。

public abstract class BaseController : AsyncController

此链接可能会进一步说明: http://msdn.microsoft.com/ en-us/library/ee728598.aspx

或这个:http://bartwullems.blogspot.com/2010/01/using-asynccontroller-in-aspnet-mvc-2.html

然后您可以将其与ajax一起使用或直接使用,鉴于它是异步的。

吉姆

Paul,

There is a AsyncController class that your base controllers can inherit from. that may be your best bet.

i.e.

public abstract class BaseController : AsyncController

this link may shed further light: http://msdn.microsoft.com/en-us/library/ee728598.aspx

or this one: http://bartwullems.blogspot.com/2010/01/using-asynccontroller-in-aspnet-mvc-2.html

You'd then use it either with ajax or directly, given that it's async.

jim

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