views:

30

answers:

2

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?

+1  A: 

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

Nick DeVore
A: 

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

jim
The Async method only works if you're server is running out of threads and you need to freeup more to do work.This doesn't increase the rendering speed of a single request.
Paul Mendoza
paul - thanks for that. i never realised that fact. kinda makes async a real edge case for the majority of web sites. i imagined it spawned an 'animated' bunch of threads to service a request queue, rather than continuing on a sequncing plod using the existing thread. interesting stuff. i must research this a little more and perhaps even refactor a bunch of my code as a result :)
jim

related questions