简体   繁体   中英

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. 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.

For lots of text, however, I'd strongly consider creating a .txt file which you embed into your assembly, and read with 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.)

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. If you're trying to get a line break in the displayed text, you should use 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. Just remember that a linebreak in the HTML itself is unlikely to be relevant in your page.

Use \\n . This is escape sequence for new lines.

Also you can use Environment.NewLine

  1. Bad solution, but better than now:
var msg="Please confirm your account by clicking this link:\n"
    + "\n"
    + @"link"+"\n";
  1. Correct way: store your strings in resource files, text-files or any other files.

You could use \\n inline in the string to indicate a new line:

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.

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());

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