简体   繁体   中英

Escaping new line characters

I'm trying to create an outlook calendar event but part of my text disappears in outlook.

private List<ICalEvent> _events;

public override void ExecuteResult(System.Web.Mvc.ControllerContext context)
{
    StringBuilder str = new StringBuilder();
    var _with1 = str;

    foreach (var _event in _events)
    {
        _with1.AppendLine("BEGIN:VCALENDAR");
        _with1.AppendLine("VERSION:2.0");
        _with1.AppendLine("BEGIN:VEVENT");
        _with1.AppendLine(string.Format("DTSTART:{0}", _event.start.ToUniversalTime().ToString("yyyyMMdd\\THHmmss\\Z")));
        _with1.AppendLine(string.Format("DTEND:{0}", _event.end.ToUniversalTime().ToString("yyyyMMdd\\THHmmss\\Z")));
        _with1.AppendLine(string.Format("SUMMARY:{0}", _event.summary));
        _with1.AppendLine(string.Format("DESCRIPTION:{0}", _event.description));
        _with1.AppendLine(string.Format("LOCATION:{0}", _event.location));
        _with1.AppendLine("END:VEVENT");
        _with1.AppendLine("END:VCALENDAR");
    }

    context.HttpContext.Response.ContentType = "text/calendar";
    context.HttpContext.Response.AddHeader("Content-disposition", string.Format("attachment; filename={0}", "newFile"));
    context.HttpContext.Response.Write(str.ToString());
}

The problem occurs when I have multiple lines in the description . If it contains the text "New\\r\\nLine" for example I will only get "New" in my outlook calendar description. The part after "\\r\\n" disappears.

I've tried all these but with the same result:

  • _event.description.Replace("\\r\\n", "\\n\\n")
  • _event.description.Replace("\\\\", "\\\\\\\\")
  • _event.description.Replace("\\r\\n", Environment.NewLine)

Anyone had a similar problem?

您需要在行尾写上 '=0D=0A'。

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