1. ホーム
  2. asp.net-mvc

[解決済み] [Solved] Replace line break characters with <br /> in ASP.NET MVC Razor view

2022-04-03 04:02:38

Question

I have a textarea control that accepts input. I am trying to later render that text to a view by simply using:

@Model.CommentText

This is properly encoding any values. However, I want to replace the line break characters with <br /> and I can't find a way to make sure that the new br tags don't get encoded. I have tried using HtmlString but haven't had any luck yet.

How to solved?

Use the CSS white-space property instead of opening yourself up to XSS vulnerabilities!

<span style="white-space: pre-line">@Model.CommentText</span>