HTML.Encode but preserve line breaks June 1, 2023 by Tarik Use HttpUtility.HtmlEncode then do the replace. @Html.Raw(HttpUtility.HtmlEncode(Message).Replace("\n", "<br/>"))