Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Html.ActionLink very slow

I am using MVC 4 and the Razor View Engine.

I have a Html.ActionLink("Title", "Action") call which is called in a foreach-loop with ~200 items and this takes about 550ms to complete. If I replace the ActionLink with a simple string it only takes ~50ms --> the Html.ActionLink needs ~500ms for 200 iterations!

Is there a way to speed this up?

I have 5 of these in my loop so my page needs >3s to render...
Release build and no debug attribute in the web.config does not help.

like image 456
Christoph Fink Avatar asked Apr 15 '13 17:04

Christoph Fink


1 Answers

I combined a few of the tips from the comments to achive a reduction from ~3s to ~250ms for the rendering of the complete page. The biggest changes where:

  • Cache the DB queries from my custom route (-> 2/3 time saving per call)
  • "Pregenerate" the links and only replace parts of it:
    I did a @{ var link = Html.ActionLink("_USER_", "Edit", new { id = "_ID_" }); } before the foreach loop and in the loop something like @link.Replace("_ID_", user.UserId.ToString()) (you can't use String.Format, as the Html.ActionLink() escapes {0} to %7B0%7D
like image 65
Christoph Fink Avatar answered Sep 27 '22 21:09

Christoph Fink