2

I'm working on displaying some text in a textarea that I've submitted via a form in my ASP.NET MVC app.

I want the line breaks to show when I'm displaying the text back.

Why does

Message.Replace(Environment.NewLine, "<br />")

not work, but

Message.Replace("\n", "<br />")

does?

Is the breakline character that the browser submits different than the windows Newline "\r\n" ?

Whooper73
  • 21
  • 1
  • 1
  • 2
  • 3
    It sounds to me like you've answered this yourself...? – Marc Gravell Dec 28 '09 at 23:09
  • 1
    @Marc Gravell, how? I'm on a windows machine. You would therefore think a newline would be "\r\n". This is the value of the Evironment.NewLine. But, this isn't the value of the newline being submitted by the browser. – Whooper73 Dec 28 '09 at 23:13

2 Answers2

7

If you take a look at this stackoverflow answer, it explains which browsers use '\r\n' and which use '\n' in javascript. I'm not sure if that would apply to sending information to a server.

Either way, Environment.NewLine is the NewLine character for the environment where your web server is running. Your software running on the web server should not assume that a client's environment is the same as yours, so you should code, assuming that sometimes you will get Environment.NewLine, and sometimes you wont.

The common denominator is the '\n', so if you use your second code option, you should handle all of your cases.

Community
  • 1
  • 1
mlsteeves
  • 1,251
  • 2
  • 16
  • 20
2

Since you must be displaying in an interface which is HTML text, the latter works. For an Outlook html body, ("\n", "<br />") works but not the former, i.e. Environment.Newline

Btw, thanks for the 2nd one. I was trying to figure out something with html.

takrl
  • 6,356
  • 3
  • 60
  • 69
Biswajeet
  • 21
  • 1