Sachin Kainth
Sachin Kainth

Reputation: 46740

Writing html to a page

I am writing some text to a page which contains some html. I write it like this

@text

But the html appears as text like this

some text. some more text.

What I'd like to happen is for the html to be rendered not as text but as actual html. So in this example, I'd like a link to appear. How would I do this?

Upvotes: 0

Views: 56

Answers (1)

Darin Dimitrov
Darin Dimitrov

Reputation: 1038720

You could use the Html.Raw method which will not HTML encode the output contrary to the @ operator:

@Html.Raw(text)

Warning: be careful with this because your site now becomes vulnerable to XSS attacks. You should do this only if you have total control over the text variable. If this variable comes directly from an user input, or a query string parameter or a form field, you might get into trouble. If you want to sanitize the user input so that it is safe to be used with the Html.Raw helper you could use the AntiXss library.

Upvotes: 3

Related Questions