【发布时间】:2014-04-23 15:21:35
【问题描述】:
我想扩展HtmlHelper,以便使用自定义属性(例如'async')呈现脚本标签
我想这样使用它
@Html.RenderBundleScript("/mybundleName", new { async = ""})
这是我的代码,它不起作用(特别是,attributes.ToString() 给出:System.Web.Routing.RouteValueDictionary 而不是 async 或 async=''):
public static IHtmlString RenderBundleScript(this HtmlHelper htmlHelper,
string bundlePath, object htmlAttributes)
{
var attributes = HtmlHelper.AnonymousObjectToHtmlAttributes(htmlAttributes);
string attributesValue = (attributes == null) ?
string.Empty : attributes.ToString();
if (string.IsNullOrEmpty(attributesValue))
{
return Scripts.Render(bundlePath);
}
else
{
//var tag = new TagBuilder("script");
// tag.MergeAttribute() ???
return Scripts.RenderFormat("<script src='{0}' " +
attributesValue +
" type='text/javascript'></script>", bundlePath);
}
}
【问题讨论】:
-
也许您可以更好地定义“不起作用”?你有任何输出或异常吗?如果有,是什么?
-
我不会来将匿名对象 {async=""} 转换为 html 属性列表 ("
async=''")。
标签: c# .net asp.net-mvc html-helper anonymous-class