简体   繁体   中英

asp.net: line break \n in resource .resx file does not work

The "\n" does not work in below code, it's just displayed as "\n"

if (!window.confirm(XXXXX.Globalization.LocalResource.InvalidUrl)) {
    return false;
}

the string is "Invalid URL! \n Are you sure you want to continue?" in resource resx file.

how to make it work?

What worked in my case was this:

This text has a<br>line break!

See the <br> there.

Look here for the details: Putting a line break in an resx resource file

I'm using Razor in ASP.NET MVC and so I used:

@Html.Raw(Localization.NotAnswered)

so that it outputs the <br> as HTML code.

Try shift-enter to create a new line in the designer. Alternatively copy and paste the result from notepad.

Another way is to edit the RESX xml manually.

The problem is that the RESX designer UI will auto-escape the strings. So your value is actually:

"Invalid URL! \\n Are you sure you want to continue?"

Use break tag "<br/>" instead of "\\n"

EDITED

This should surely work

use "\r\n"

I used shift+enter, it think it's the simplest.

< br >certainly will not work, have not tried \r\n, but since \n does not work, I am not very confident with \r\n

ASP.NET MVC

STEP-1
In my Resource.resx file put Shift+Enter between the paragraph1 & paragraph2
My text paragraph1
My text paragraph2

STEP-2
In my view page its

<p>@Html.Raw(Regex.Replace(Resource.Mytext, "\r\n", "<br />"))</p>

Short answer: use &lt;br&gt; if editing raw file or use <br> if editing in IDE.

Details: If you create line breaks by just pressing Enter or Shift+Enter then the line breaks will not show up on a web page. Going back to HTML basics, if you want the browser to render line breaks then you must use a <br> tag. For me I was editing my resx file using a simple text editor, and the problem I encountered when adding a <br> tag is that when I later tried to open the file in Visual Studio it threw an error about " the closing tag doesn't match ". It turns out that resx files don't allow you to use a <br> tag. You need to actually use &lt;br&gt; . In fact, if you're editing the file in Visual Studio and you enter <br> in the value field, it will automatically convert <br> to &lt;br&gt; . You won't see it in the IDE, but if you enter <br> in the IDE, save it, then open the raw file in Notepad++ you will see the actual value is &lt;br&gt; .

当您输入字符串时,Shift-enter 效果很好,但是如果您已经创建了资源文件,并且您不想编辑每个字符串,您可以这样做

String myStringFromResx = myStringFromResx.Replace("\\n," "\n");

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM