Search code examples
c#htmlasp.net-mvcwhitespace

rendering html as plaintext without stripping whitespace


Basically I am pulling an HTML file from a server and displaying the source code in the browser, including the tags. Currently, i pull the file down as a stream and convert it to a string:

StreamReader reader = new StreamReader(item.DownloadFile());
string raw = reader.ReadToEnd();
file.Contents = raw;

And then

HttpUtility.HtmlEncode(file.Contents);

which is displayed in my view using

@Html.Raw(Model.Contents)

This works fine for displaying all of the source. HOWEVER the output is a single block of text, completely stripped of whitespace, and thus is not easily readable, which defeats the purpose of me displaying the source code.

How can i output the source code such that indentation and newlines remain in tact?


Solution

  • Either wrap your code in

    <pre> 
        @Html.Raw(Model.Contents)
    </pre>
    

    Or using CSS, add the following rule:

    white-space: pre;