2

I have two different views that makes upwards of 500 partial view calls each to a common partial view. Good design tells me that I should leave the partial view where it is and reference it from both overlying views to prevent code duplication. Unfortunately, performance suffers - copy-pasting the partial view in each of the other two views yields a 300ms improvement.

Is there anyway that I can, include a partial view in an overlying view, reaping the performance benefits of not using the actual Partial() call, while at the same time not having to maintain duplicate code? Note - I realize that I could write some sort of VS add-on that would copy-paste the view code, but I am looking for other options...

Jason Fry
  • 1,204
  • 12
  • 24
  • 2
    Good design tells **me** don't have 500+ partial views... **:-)** – gdoron Jan 19 '12 at 02:20
  • I would tend to agree with that! Regardless, I don't have much choice in this case. – Jason Fry Jan 19 '12 at 02:23
  • 2
    Are the RenderPartial() calls in a loop? If yes then its better to move the iteration to the Partial and make as few RenderPartial() calls as possible. You can also think about creating an HTML helper for the Partial Also test things in "Release" mode as the HtmlHelper.RenderPartial() does not cache the route while in Debug. – IUnknown Jan 19 '12 at 03:05

2 Answers2

3

Things to try that might improve performance:

  • Use @{Html.RenderPartial("_foo");} instead of @Html.Partial("_foo") to include the partial
  • Always do your benchmarking in Release mode. Lots of optimizations and caching are performed by ASP.NET MVC in contrast to Debug mode.

If the previous 2 suggestions didn't yield the required performance then you might try replacing the partial with a custom HTML helper that will generate the HTML fragment. This could work if the HTML of the partial is not very complex.

Darin Dimitrov
  • 1,023,142
  • 271
  • 3,287
  • 2,928
  • I am building in release, and the RenderPartial optimization didn't do anything. I will make this into an HTML helper, however. Thanks. – Jason Fry Jan 19 '12 at 16:34
0

I'm going to date myself here, but have you considered using a server side include to inject the partial view's code, rather than calling out to it? I have absolutely no evidence, empirical or subjective, that this would be any faster. Just something worth trying.

Chris Shain
  • 50,833
  • 6
  • 93
  • 125
  • Apparently razor does not support server side includes. http://stackoverflow.com/questions/5314476/how-do-you-include-html-or-asp-file-using-razor – Jason Fry Jan 19 '12 at 16:17