發生這種情況的原因是因爲Razor中的@
運算符自動進行HTML編碼。如果你想避免這種編碼,你需要使用一個IHtmlString
:
public static IHtmlString ImageLink(
this HtmlHelper htmlHelper,
string imgSrc,
string alt,
string actionName,
string controllerName,
object routeValues,
object htmlAttributes,
object imgHtmlAttributes
)
{
return MvcHtmlString.Create(@"<img src=""../../Content/images/english.png"" />");
}
這顯然會更正確的(和工作在所有情況下,沒有從那裏,這幫助是如何調用物質),如果這樣寫:
public static IHtmlString ImageLink(
this HtmlHelper htmlHelper,
string imgSrc,
string alt,
string actionName,
string controllerName,
object routeValues,
object htmlAttributes,
object imgHtmlAttributes
)
{
var img = new TagBuilder("img");
var urlHelper = new UrlHelper(htmlHelper.ViewContext.RequestContext);
img.Attributes["src"] = urlHelper.Content("~/Content/images/english.png");
// Don't forget that the alt attribute is required if you want to have valid HTML
img.Attributes["alt"] = "English flag";
return MvcHtmlString.Create(img.ToString(TagRenderMode.SelfClosing));
}
然後
@Html.ImageLink("../../Content/images/english.png","English", "ChangeCulture", "Account", new { lang = "en", returnUrl = this.Request.RawUrl }, null,null)
將正常工作。
好像不能修改,你可以使用@Html.Raw
助手替代:
@Html.Raw(Html.ImageLink("../../Content/images/english.png","English", "ChangeCulture", "Account", new { lang = "en", returnUrl = this.Request.RawUrl }, null,null))
謝謝您的回答,我投您太:) – 2011-05-04 19:53:44
的MvcHtmlString()創造了奇蹟爲我。謝謝 – 2013-10-18 16:58:32