在C#和ASP.NET线路中断的变化是什么? (\\ r \\ n相对于\\ N) [英] What's with the line break variations in C# and ASP.NET? (\r\n vs.\n)
问题描述
我已经写code为ASP.NET启动以来,今天我遇到了一些我以前从未见过。通常情况下,我期望的是C#换行符被期待(从一个textarea在ASP.NET发布时为例)为\\ r \\ n。现在,我使用的MVC框架,并过来的电线从一个textarea文本只是有\\ n表示换行。
I've been writing code for ASP.NET since the start, and today I encountered something I've never seen before. Typically I've looked for line breaks in C# (when posted in ASP.NET from a textarea, for example) by expecting "\r\n". Now I'm using the MVC framework, and the text coming over the wire from a textarea simply has "\n" for line breaks.
有什么在标准化换行符老同学TextBox控件怎么回事?由于MVC只使用表单数据作为-IS,这正常吗?希望能得到一些启示。
Is there something going on in old school TextBox controls that normalizes the line breaks? Since MVC just uses the form data as-is, is this normal? Would love to get some insight.
推荐答案
我已经观察到的实际换行符序列从浏览器的不同而不同的浏览器。
I have made observation that the actual line break sequence differs from browser to browser.
如果你有一个页面上的多行文字区域,并提交页面,则:
If you have a multiline textarea on a page and the page is submitted then:
IE浏览器返回\\ r \\ n表示换行。 FF返回\\ n,在这种情况下。
IE returns "\r\n" to indicate newlines. FF returns "\n" in this case.
我测试了它某处2006年底,所以现在可能是不同的。
I tested it somewhere along the end of 2006, so it may be different now.
我不相信它可能有什么关系的WebForms与MVC。只是双方提交过程的输入,并返回到你,因为它是。
I do not believe it could have anything to do with WebForms vs. MVC. Both just process submitted input and return it to you as it is.
如果您希望以某种方式处理和将这些字符替换它将使感,在长到短的时间做它:
If you wish to somehow process and replace these characters it would make sense doing it in the long-to-short order:
string userText = form["Text"];
userText = userText.Replace ("\r\n", "<br/>").Replace ("\r", "<br/>");
这篇关于在C#和ASP.NET线路中断的变化是什么? (\\ r \\ n相对于\\ N)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!