N. Sch N. Sch - 22 days ago 13
ASP.NET (C#) Question

Making HTML show up as a link

I am trying to display messages from one user to another on the site. Messages are saved in a database with the HTML.

Why is the HTML on the site, showing up, instead of turning the HTML into a link or a line break?

Here is the code:

<div class="jumbotron">
<h4><u>Your Messages</u></h4>
@foreach (var m in Model.User.MessagesUsers)
{
<p>@m.Message</p>
}
</div>

Answer

It looks like you're using ASP.NET MVC. The framework automatically HTML-encodes the output for security reasons.

You can get around that by using @Html.Raw:

<p>@Html.Raw(m.Message)</p>

Be aware however that this is circumventing security. Make sure you trust your data. This is how cross-site scripting attacks happen. You might also want to look into something like Markdown to allow users to enter markup over which you have more control.