Ido Ran
Ido Ran

Reputation: 11404

Debug Slow Razor View

I have ASP MVC 4 web application. One of the actions return very slowly. I use MiniProfiler to profile the application.

We handle the duration of the action itself which is now 14ms, the problem is that there is still about 1.5s on the step of the request itself, without the time of the action on the controller (please see the attached image).

MiniProfiler screen shoot

As you can see the first line duration (1262.3) is the duration without the children. As far as I understand this is the time of the razor engine rending. It is important to note the slowness persist, it is not just the first request. It never goes bellow 800ms and sometime up-to 2s.

How can I profile the rendering itself? The view is quite complex with several partial views in it.

Upvotes: 4

Views: 2136

Answers (3)

Ido Ran
Ido Ran

Reputation: 11404

I've create a small class that inherit from RazorViewEngine and add profiling using MiniProfiler.

This help me to see which view or partial view take the longest to render. It does not explain why, for example due to usage of ActionLink or long calculation.

Upvotes: 0

Mathias F
Mathias F

Reputation: 15911

See if Glimpse gives you more insight

http://getglimpse.com/

Rendering a View can take very long if Routes are calculated. Check how long rendering takes if you remove all ActionLinks and similiar Html Helpers.

Upvotes: 1

Darin Dimitrov
Darin Dimitrov

Reputation: 1039110

Looks like a SQL query is being executed in your view. This could happen if you are using some ORM framework such as EntityFramework which is lazily loading entities and they are eagerly fetched from the database once you touch at their properties (which happens in your view). I I would recommend you using view models and eagerly loading everything in the controller action instead of waiting this to happen in your view.

Upvotes: 3

Related Questions