Bob Paksi
Bob Paksi

Reputation: 23

MVC - Output HTML from code, better to use Response.OutputStream or StringBuilder

I'm writing a asp.net MVC site and I have written some extensions to the Html class to generate some html for me.

From within the extension method is it better to write directly to the Response.Output stream or have the extension method return a string?

What are the advantages / disadvantages of using the Reponse.Output stream directly from a View.

Usage example:

<%= Html.GenerateHtml() %>

vs.

<% Html.GenerateFoo() %>

From within the GenerateFoo() method I can writ directly to the output streem with the following

... 
helper.ViewContext.HttpContext.Response.OutputStream.Write()
...

Upvotes: 2

Views: 1697

Answers (2)

Todd Smith
Todd Smith

Reputation: 17282

Returning a string gives you the option of modifying/inspecting/capturing the result before it gets dumped to Response.Write().

Upvotes: 3

chadmyers
chadmyers

Reputation: 3810

First, I wouldn't use OutputStream for text output, I would use Response.Write().

Second, returning a string to <%= calls Response.Write() anyhow (look at the compiled output of an ASPX in your Temp ASP.NET files folder sometime).

Upvotes: 0

Related Questions