How to print HTML objects using String.Format

I have something like this inside my view:

@String.Format("{0} © Copyright by Nemanja", DateTime.Now.Year); 

However, this will elude my & in & . Is there any way around this?

+4
source share
3 answers

You are looking for @Html.Raw(...) that will prevent this and create an XSS hole.

In your case, you can completely get rid of it:

 @DateTime.Now.Year © Copyright by Nemanja 
+9
source

You can:

 @Html.Raw(String.Format("{0} © Copyright by Nemanja", DateTime.Now.Year)); 
+5
source

Close it in Html.Raw to prevent escaping. Almost all helpers in MVC will encode HTML content to ensure that any model values ​​that could contain HTML or scripts cannot potentially include dangerous scripts. Html.Raw explicitly displays content without encoding it.

 @Html.Raw( String.Format("{0} © Copyright by Nemanja", DateTime.Now.Year) ) 
+3
source

Source: https://habr.com/ru/post/1502491/


All Articles