c# - 在创建自定义图像 Html 帮助程序时,它会抛出 System.ArgumentNullException: 'Value cannot be null。参数名称:virtualPath'
问题描述
在创建自定义图像 Html Helper 时它会抛出
“System.ArgumentNullException:'值不能为空。参数名称:virtualPath'”
public static IHtmlString Image(this HtmlHelper helper, string src, string alt, string height, string width, params string[] allClasses)
{
TagBuilder tb = new TagBuilder("img");
tb.Attributes.Add("src", VirtualPathUtility.ToAbsolute(src));//throws error
tb.Attributes.Add("alt", alt);
if (!string.IsNullOrEmpty(height) || !string.IsNullOrEmpty(width))
{
StringBuilder value = new StringBuilder();
if (!string.IsNullOrEmpty(height))
value.AppendFormat("height:{0};", height);
if (!string.IsNullOrEmpty(width))
value.AppendFormat("width:{0};", width);
tb.Attributes.Add("style", value.ToString());
}
if (allClasses?.Any() ?? false)
tb.Attributes.Add("class", string.Join(" ", allClasses));
return new MvcHtmlString(tb.ToString(TagRenderMode.SelfClosing));
}
解决方案
推荐阅读
- android - REST API 在 Postman 中给出了正确的响应,但给出了 Volley ServerError
- xml - 如何将 QML 之类的声明性语言解析为 QWidgets .ui XML 格式代码?
- git - 本地网络中的源代码控制
- database - Aerospike 集群配置
- javascript - 如何将 CJS 模块要求到本机 Node 中的 ES6 模块中?
- php - 如何在 wordpress 上创建 elementor 的小部件类别
- c - Why does my code print out some garbage from my array?
- python - 如何在 Pygame 中获取精灵的位置?
- symfony - 从 Symofny 中的数组中检索数据
- android - 如何解决“Manifest merge failed : Attribute application@appComponentFactory”的错误