Asp.net mvc 在TagBuilder.GenerateID中使用GUID字符串
我使用本文创建了一个图像帮助器: . 它说它取代了句号,但没有提到其他限制 除非我尝试使用GUID字符串作为id,否则一切正常 剃刀代码:Asp.net mvc 在TagBuilder.GenerateID中使用GUID字符串,asp.net-mvc,asp.net-mvc-3,asp.net-mvc-2,Asp.net Mvc,Asp.net Mvc 3,Asp.net Mvc 2,我使用本文创建了一个图像帮助器: . 它说它取代了句号,但没有提到其他限制 除非我尝试使用GUID字符串作为id,否则一切正常 剃刀代码: @Html.Image(id, ....) //passes a GUID string to helper fine ImageHelper与网页描述的完全相同: builder.GenerateId(id); //breakpoint after this shows the builder object //does not create an
@Html.Image(id, ....) //passes a GUID string to helper fine
ImageHelper与网页描述的完全相同:
builder.GenerateId(id);
//breakpoint after this shows the builder object
//does not create an id attribute when id is a GUID string.
//using id.ToString() doesn't work for GUIDs either
在MergeAttributes中使用GUID字符串可以正常工作:
builder.MergeAttribute("title", id); //the GUID string is img title no problem
builder.MergeAttribute("class", id); //the GUID string is class as expected
当使用GUID字符串作为ID时,是否有一些限制或解决方法?ID必须以字母而不是数字开头。通常,GUID以数字开头,因此会出现问题
TagBuilder.GenerateId
调用createSanitizeId
,执行此检查:
if (!TagBuilder.Html401IdUtil.IsLetter(c1))
return (string) null;
最好是在ID的开头添加前缀,如“guid”或其他内容。ID必须以字母开头,而不是数字。通常,GUID以数字开头,因此会出现问题
TagBuilder.GenerateId
调用createSanitizeId
,执行此检查:
if (!TagBuilder.Html401IdUtil.IsLetter(c1))
return (string) null;
最好只在ID的开头添加前缀,如“guid”或其他内容。guid不是字符串,而是结构。尝试使用Guid.ToString('N'),它会为您提供一个没有大括号或连字符的字符串Guid不是字符串,而是一个结构。试试Guid.ToString('N'),它会给你一个没有大括号或连字符+1的字符串,我喜欢它-我的意思是前缀-简单的解决方案是最好的。谢谢你,柯克,我想我以前从来没有试过用数字作为ID的开头。+1我喜欢它——我的意思是前缀——简单的解决方案是最好的,嗯?@PreetSangha。谢谢你,柯克,我想我以前从来没有试过用一个数字来做一个ID。