Why do ASP.NET MVC HTML Helpers return MvcHtmlString instances? It seems rather inefficient to allocate a string for each rendered helper in a page. Why don't Helpers work the same way as WebForms Controls by rendering directly to a HtmlTextWriter?
Asked
Active
Viewed 265 times
0
-
Note that you are generally allocating strings even when you are just outputting them to a stream. – Andrew Barber Mar 31 '12 at 03:45
2 Answers
1
Returning a string allows you to potentially do more with it, or to call them in other places (such as helper classes).
If you want the perf, there are versions of some of them that write directly, such as RenderPartial()
. See this stack overflow question: Html.Partial vs Html.RenderPartial & Html.Action vs Html.RenderAction
edit: and this one: What is the difference (if any) between Html.Partial(view, model) and Html.RenderPartial(view,model) in MVC2?
1
MVC Views can be used not only to produce HTML, but as a templating system used, for example, to produce e-mails. You will easily find here, on StackOverflow, method to render view directly to string. This also gives you easier possibility to test your views.

LukLed
- 31,452
- 17
- 82
- 107