danze
danze

Reputation: 765

Retain string formatting in the response from asp.net mvc2 action method

I'm trying to show test run log (text file in a windows server) to the user if he/she clicks a link.

So far I'm able to return the string but the formatting is lost (no newlines) when displayed in the browser.

public string GetTestLog(...)
{ 
    return File.ReadAllText(logFileUrl);
}

How can I keep the formatting of the string?

Upvotes: 1

Views: 169

Answers (2)

amit_g
amit_g

Reputation: 31270

You could return the file with test/plain contentType and then it should render properly as a text file...

public ActionResult GetTestLog(...)
{
    return new FileContentResult(File.ReadAllBytes(logFileUrl), "text/plain");
}

If you are rendering it as HTML, you have to replace all newlines with <br/> or enclose it in a TEXTAREA or PRE.

Upvotes: 1

Tim M.
Tim M.

Reputation: 54417

You can display the output inside ah HTML PRE tag to preserve the original formatting.

http://www.w3.org/TR/html5/the-pre-element.html#the-pre-element

Upvotes: 0

Related Questions