If I have en extension like that :
public static string ImageLink(this HtmlHelper htmlHelper,
string imgSrc,
string alt,
string actionName,
string controllerName,
object routeValues,
object htmlAttributes,
object imgHtmlAttributes)
{
return @"<img src=""../../Content/images/english.png"" /> ";
}
and I use it in a partial view like this :
@Html.ImageLink("../../Content/images/english.png","English", "ChangeCulture", "Account", new { lang = "en", returnUrl = this.Request.RawUrl }, null,null)
I have an output like this :
Any idea why?
Have it return
MvcHtmlString
instead (my sample below).The reason this happens is because the
@
operator in Razor automatically HTML encodes. If you want to avoid this encoding you need to use anIHtmlString
:which obviously will be far more correct (and working in all situations, no matter from where and how this helper is called) if written like this:
and then
will work properly.
As an alternative if you cannot modify the helper you could use
@Html.Raw
: