How to include a new line in a constant in C #

I have a code that sends an email to users upon 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 much more advanced, and I saw a lot of patterns. However, all templates there have a length of at least 100 lines and have new lines after each line. Here is an example of when I tried to add only 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, I get a message saying that I can not include a new line in the constant.

Can someone suggest another way that I could include doing this?

+7
source share
4 answers

There are three questions here. First, if you have a lot of text, you should not include this directly in the source code. For small localizable snippets of text, you can use the resx / resources file - Visual Studio will present you with a grid so that you can specify text for a specific resource name, etc.

However, for large text, I highly recommend creating a .txt file that you insert into your assembly and reading it using Assembly.GetManifestResourceStream . Editing a text file is much easier than managing large blocks of string literals.

The rest of the answer, however, solves the question you really asked about string literals.

The second is a line break in a line, which can be done either by directly including it 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 the carriage return and the line. Sometimes you may need only \r or just \n . It depends on the context.)

Or shorthand 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 the text literal of a string, you need to avoid double quotes by doubling them, since the backslash is just a backslash.

But that just gives you a line break in your HTML. If you are trying to get a line break in the displayed text, you should use HTML. For example, you can 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 think the <br> tag is basically out of order - you should look at other ways to control your HTML layout. Just remember that a line in the HTML itself is unlikely to be relevant on your page.

+14
source share

Use \n . This is an escape sequence for newlines.

You can also use Environment.NewLine

+3
source share
  • Bad solution, but better than now:
     var msg = "Please confirm your account by clicking this link: \ n"
     + "\ n"
     + @ "link" + "\ n";

  1. The right way: store your lines in resource files, text files, or any other files.
+2
source share

You can use the string \n inline in a line 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 most of the text to send, you can use StringBuilder to create lines of text and then write this 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()); 
+1
source share

All Articles