ASP.Net WebForms 中的 Gravatar 显示默认图像

Gravatar in ASP.Net WebForms showing default image

我在每个循环的博客评论中嵌入了一个 AjaxToolKit Gravatar。 Gravatar 的电子邮件地址并不代表电子邮件地址的实际值,而是文字字符串(蓝色字体) CommentEmail。我尝试了多种语法选项,但尚未成功。这是目前的代码:

<div class="comments">
    <div>
        <h3>Comments</h3>
    </div>

        <%  foreach (System.Data.DataRow myCRow in myCommentDataTable.Rows)
            {
                string CommentID = myCRow[0].ToString();
                string BlogParentID = myCRow[1].ToString();
                string Commenter = myCRow[2].ToString();
                string CommenterEmail = myCRow[3].ToString();
                string Comment = myCRow[4].ToString();
                string CommentDate = myCRow[5].ToString();
                if (BlogParentID == Id)
                {
                    Response.Write("<div class='commentdate pull-right'>" + CommentDate + "</div>");
                    Response.Write("<div class='commenter'>" + Commenter + "</div><br />");
                    Response.Write("<p>" + Comment + "</p>");
                    %><Ajax:Gravatar runat='server' ID='GravatarImage' Email='<%= CommenterEmail  %> ' DefaultImageBehavior='MysteryMan' /><br /><br /><%
                }
            }%>
</div> 

最后一行中的 CommenterEmail 是有问题的文字。它显示为蓝色字体的字符串而不是黑色字体的变量。因此它评估为无效的电子邮件地址并显示默认的 "myster man" 图标。如果我在那里硬编码我的电子邮件地址,它会正常工作,为所有用户显示我的 Gravatar。

将电子邮件值嵌入 AjaxToolKit:Gravatar 的正确语法是什么?

经过两周与 Ajax Gravatar 控件的斗争,我得出的结论是它根本不适用于动态生成的电子邮件地址。

我是这样实现的...

在aspx中:

<div class="comments">
    <div>
        <h3>Comments</h3>
    </div>
        <%
            foreach (System.Data.DataRow myCRow in myCommentDataTable.Rows)
            {
                string CommentID = myCRow[0].ToString();
                string BlogParentID = myCRow[1].ToString();
                string Commenter = myCRow[2].ToString();
                string CommenterEmail = myCRow[3].ToString().ToLower().Trim();
                string Comment = myCRow[4].ToString();
                string CommentDate = myCRow[5].ToString();

                if (BlogParentID == Id)
                {
                    //  Compute the hash
                    string hash = HashEmailForGravatar(CommenterEmail);
                    //  Assemble the url and return
                    string myGravatar = string.Format("http://www.gravatar.com/avatar/{0}?size=50", hash);
                    Response.Write("<div class='commentdate pull-right'>" + CommentDate + "</div>");
                    Response.Write("<div class='commenter'>");
                    Response.Write("<img src='" + myGravatar + "' alt='" + Commenter + "' />&nbsp;&nbsp;");
                    Response.Write(Commenter + "</div>");
                    Response.Write("<p>" + Comment + "</p>");

                }
            }%>
</div> 

在代码隐藏中:

public static string HashEmailForGravatar(string email)
{
    // Create a new instance of the MD5CryptoServiceProvider object.  
    MD5 md5Hasher = MD5.Create();

    // Convert the input string to a byte array and compute the hash.  
    byte[] data = md5Hasher.ComputeHash(System.Text.Encoding.Default.GetBytes(email));

    // Create a new Stringbuilder to collect the bytes  
    // and create a string.  
    System.Text.StringBuilder sBuilder = new System.Text.StringBuilder();

    // Loop through each byte of the hashed data  
    // and format each one as a hexadecimal string.  
    for (int i = 0; i < data.Length; i++)
    {
        sBuilder.Append(data[i].ToString("x2"));
    }

    return sBuilder.ToString();  // Return the hexadecimal string. 
}

Dan's Blog