[英]ASP.NET MVC 3: Extension method on UrlHelper/HtmlHelper encodes to HTML
我已經為UrlHelper編寫了一些擴展方法,以便更輕松地加載或標記。 但是,它似乎在瀏覽器中呈現為原義文本。 這是我所擁有的:
public static string Script(this UrlHelper helper, string scriptPath)
{
return string.Format(@"<script src=""{0}"" type=""text/javascript""></script>", helper.Content(scriptPath));
}
這是我的.cshtml代碼:
@section HeadContent
{
@Url.Style("MyStyleName")
@Url.Script("MyScriptName")
@Url.MetaKeywords("My Keywords")
@Url.MetaDescription("Some Description")
}
並以<script [etc, etc]>
在瀏覽器中顯示
如果我不使用擴展方法,它將按預期正常運行...如何使它與擴展一起使用?
嘗試這個:
public static string Script(this UrlHelper helper, string scriptPath)
{
return MvcHtmlString.Create(string.Format(@"<script src=""{0}"" type=""text/javascript""></script>", helper.Content(scriptPath)));
}
所有HTML幫助程序都需要返回MvcHtmlString。 如果僅返回一個字符串,則它將被視為不受信任的值,並將進行HTML編碼。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.