One of my error message renders a link. However, Html.ValidationSummary()
encodes it and therefore it displays as follow:
An account with the mobile or email you have specified already exists. If you have forgotten your password, please <a href="/account/Reset">Reset</a> it.
Instead, it should render as:
An account with the mobile or email you have specified already exists. If you have forgotten your password, please Reset it.
The error is added to the ModelState inside view as follows:
if (...)
{
ViewData.ModelState.AddModelError(string.Empty, string.Format("An account with the mobile or email you have specified already exists. If you have forgotten your password, please {0} it.", Html.ActionLink("Reset", "Reset")));
}
In short, how should I prevent Html.ValidationSummarry()
to selectively/entirely encoding html in errors.
The current HTML helpers for displaying error messages do not support this. However, you could write your own HTML helpers that display the error message without HTML escaping it, i.e. they would treat the error message as raw HTML.
As a starting point, you could use the ASP.NET MVC source code from Codeplex, specifically the
ValidationSummary
method of theValidationExtensions
class:You can then change this method to treat the error message as raw HTML.
Two warnings though:
You're changing the meaning of certain properties of the
ModelState
class. While you get away with using your own HTML helpers now, a future version of ASP.NET MVC might introduce changes that no longer work with this approach.Be very careful about not using error messages that aren't properly escaped so you don't expose your web app to XSS attacks. Certain standard validation annotation might not work any longer since they don't HTML escape the error message.