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

时间:2017-11-13 21:05:15

标签: asp.net ajaxcontroltoolkit gravatar

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

Ex:
If your missing resoruce is:
https://dl.google.com/dl/android/maven2/com/android/support/test/espresso/espresso-core/3.0.1/espresso-core-3.0.1.pom

Download it and put it in:
C:/Program Files/Android/Android Studio/gradle/m2repository/com/android/support/test/espresso/espresso-core/3.0.1/espresso-core-3.0.1.pom

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

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

1 个答案:

答案 0 :(得分:0)

在与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