[英]How can I include a newline in a constant in C#
I have code that sends an email message to users on registration: 我有代码在注册时向用户发送电子邮件:
await UserManager.SendEmailAsync(2, "Confirm your account",
"Please confirm your account by clicking this link: <a href=\"www.cnn.com\">link</a>");
This works but I want to do something a lot more advanced and I have seen many templates out there. 这有效,但我想做更高级的事情,我看到很多模板。 However all templates out there are at least 100 lines long and have newlines after every line.
但是,所有模板都至少有100行,每行后都有换行符。 Here's an example of when I tried adding just one new line.
这是我尝试添加一个新行时的示例。
await UserManager.SendEmailAsync(2, "Confirm your account",
"Please confirm your account by clicking this link:
<a href=\"www.cnn.com\">link</a>");
As soon as I have a new line then I get a message saying I cannot include a new line in a constant. 一旦我有一个新的行,然后我收到一条消息,说我不能在常量中包含一个新行。
Can anyone suggest another way that I could include do this? 任何人都可以提出另一种方法,我可以包括这样做吗?
There are three issues here. 这里有三个问题。 The first is that if you have a lot of text, you shouldn't be including that in the source code directly anyway.
首先,如果你有很多文本,你不应该直接在源代码中包含它。 For small localizable pieces of text, you can use a resx/resources file - Visual Studio will present you with a grid so you can specify text for a particular resource name etc.
对于可本地化的小文本,您可以使用resx / resources文件 - Visual Studio将为您提供网格,以便您可以为特定资源名称等指定文本。
For lots of text, however, I'd strongly consider creating a .txt
file which you embed into your assembly, and read with Assembly.GetManifestResourceStream
. 但是,对于大量文本,我强烈考虑创建一个嵌入到程序集中的
.txt
文件,并使用Assembly.GetManifestResourceStream
读取。 It's much easier to edit a text file than to manage large blocks of string literals. 编辑文本文件要比管理大块字符串文字要容易得多。
The rest of the answer, however, addresses the question you actually asked, about string literals. 然而,答案的其余部分解决了您实际问过的关于字符串文字的问题。
Second is getting a line break into the string, which can be done either by including it directly using escape sequences: 第二是在字符串中进行换行,可以通过直接使用转义序列来完成:
await UserManager.SendEmailAsync(2, "Confirm your account",
"Please confirm your account by clicking this link:\r\n<a href=\"www.cnn.com\">link</a>");
(Here \\r\\n
is a carriage return and line feed. Sometimes you may want just \\r
or just \\n
. It depends on the context.) (这里
\\r\\n
是一个回车和换行符。有时你可能只需要\\r
或\\n
。它取决于上下文。)
Or a verbatim string literal: 或逐字字符串文字:
await UserManager.SendEmailAsync(2, "Confirm your account",
@"Please confirm your account by clicking this link:
<a href=""www.cnn.com"">link</a>");
Note that in a verbatim string literal, you need to escape double-quotes by doubling them, as a backslash is just a backslash. 请注意,在逐字字符串文字中,您需要通过加倍来引用双引号,因为反斜杠只是反斜杠。
But that will just give you a line break in your HTML. 但这只会让你在HTML中有一个换行符。 If you're trying to get a line break in the displayed text, you should use HTML.
如果您尝试在显示的文本中获取换行符,则应使用HTML。 For example, you could use:
例如,您可以使用:
await UserManager.SendEmailAsync(2, "Confirm your account",
"Please confirm your account by clicking this link:<br /><a href=\"www.cnn.com\">link</a>");
... but I gather the <br>
tag is mostly out of favour - you should look at other ways of controlling the layout of your HTML. ...但我收集的
<br>
标签大多不受欢迎 - 你应该看看控制HTML布局的其他方法。 Just remember that a linebreak in the HTML itself is unlikely to be relevant in your page. 请记住,HTML本身的换行符不太可能与您的页面相关。
Use \\n
. 使用
\\n
。 This is escape sequence for new lines. 这是新行的转义序列。
Also you can use Environment.NewLine
您也可以使用
Environment.NewLine
var msg="Please confirm your account by clicking this link:\n" + "\n" + @"link"+"\n";
You could use \\n
inline in the string to indicate a new line: 您可以在字符串中使用
\\n
内联来表示新行:
await UserManager.SendEmailAsync(2, "Confirm your account",
"Please confirm your account by clicking this link:\n\n<a href=\"www.cnn.com\">link</a>");
Alternatively, and probably better if you have a large piece of text to send, you can use StringBuilder
to build up lines of text, then write that to your method. 或者,如果您要发送大量文本可能会更好,您可以使用
StringBuilder
构建文本行,然后将其写入您的方法。
var builder = new StringBuilder();
builder.AppendLine("Please confirm your account by clicking this link:");
builder.AppendLine("<a href=\"www.cnn.com\">link</a>")
await UserManager.SendEmailAsync(2, "Confirm your account", builder.ToString());
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.