1) Writing a function to enter the razor code with @ to prevent razor injection is a kind of hacker solution. Itβs like adding icing to a burnt house to make it better. Why not just prevent it in the first place?
Instead, try this in the appropriate view:
<%=Html.Encode(feedback.Message)%>
or add
<text> ... </text>
to the inputs before they become processes.
2) If you can guarantee that it is a legitimate email address, then everything will be fine. But it is difficult to guarantee. Therefore, I suggest that you use simple but effective email validation using JavaScript regex. This is a logical function that you can put in your JavaScript to check email (this regular expression has a success rate of ~ 95%):
function isValidEmailAddress(emailAddress) { var emailPattern = new RegExp(/^[-a-z0-9~!$%^&*_=+}{\'?]+(\.[-a-z0-9~!$%^&*_=+}{\'?]+)*@([a-z0-9_][-a-z0-9_]*(\.[-a-z0-9_]+)*\.(aero|arpa|biz|com|coop|edu|gov|info|int|mil|museum|name|net|org|pro|travel|mobi|[az][az])|([0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}))(:[0-9]{1,5})?$/i); return emailPattern.test(emailAddress); }
source share