我想显示在我的应用程序所有页面上登录的用户的图像。我已经为用户详细信息成功创建了CRUD,但还希望在所有页面上显示名称及其图片。我只能检索名称。

如何获取图片?

_LoginPartial.cshtml:

@if (Request.IsAuthenticated)
{
    Convert.ToString(ViewBag.EmployeeName);

    <text> Hello, @Html.ActionLink(User.Identity.Name, "Manage", "Account",routeValues: null, htmlAttributes: new { @class = "username", title = "Manage" })
    @using (Html.BeginForm("LogOff", "Account", FormMethod.Post, new { id = "logoutForm" }))
    {
        @Html.AntiForgeryToken()
        <a href="javascript:document.getElementById('logoutForm').submit()">Log off</a>
    }
    </text>
}
else
{
    <ul>
    <li>@Html.ActionLink("Log in", "Login", "Account", routeValues: null, htmlAttributes: new { id = "loginLink" })
    </li>
    </ul>
}

注意:

我正在使用asp.net MVC 4(实体框架)。

最佳答案

您将为此编写一个帮助器类,我将告诉您有关Gravatar Images的实现方法:

添加类别GravatarOptions:

public class GravatarOptions
{
    public string DefaultImageType { get; set; }
    public string RatingLevel { get; set; }
    public int Size { get; set; }
    public string CssClass { get; set; }

    public class DefaultImage
    {
        public const string Default = "";
        public const string Http404 = "404";
        public const string MysteryMan = "mm";
        public const string Identicon = "identicon";
        public const string MonsterId = "monsterid";
        public const string Wavatar = "wavatar";
        public const string Retro = "retro";
    }

    public class Rating
    {
        public const string G = "g";
        public const string PG = "pg";
        public const string R = "r";
        public const string X = "x";
    }


    internal static GravatarOptions GetDefaults()
    {
        return new GravatarOptions
        {
            DefaultImageType = GravatarOptions.DefaultImage.Retro,
            Size = 150,
            RatingLevel = GravatarOptions.Rating.G
        };
    }
}

添加类别GravatarHelper:
public static class GravatarHelper
{
    public static HtmlString GravatarImage(this HtmlHelper htmlHelper, string emailAddress, GravatarOptions options = null)
    {
        if (options == null)
            options = GravatarOptions.GetDefaults();

        var imgTag = new TagBuilder("img");

        emailAddress = string.IsNullOrEmpty(emailAddress) ? string.Empty : emailAddress.Trim().ToLower();

        if (!string.IsNullOrEmpty(options.CssClass))
        {
            imgTag.AddCssClass(options.CssClass);
        }

        imgTag.Attributes.Add("src", string.Format("http://www.gravatar.com/avatar/{0}?s={1}{2}{3}",
        GetMd5Hash(emailAddress),
            options.Size,
            "&d=" + options.DefaultImageType,
            "&r=" + options.RatingLevel
            )
        );

        return new HtmlString(imgTag.ToString(TagRenderMode.SelfClosing));
    }

    // Source: http://msdn.microsoft.com/en-us/library/system.security.cryptography.md5.aspx
    private static string GetMd5Hash(string input)
    {
        byte[] data = MD5.Create().ComputeHash(Encoding.UTF8.GetBytes(input));
        var sBuilder = new StringBuilder();
        for (int i = 0; i < data.Length; i++)
        {
            sBuilder.Append(data[i].ToString("x2"));
        }
        return sBuilder.ToString();
    }
}

将此添加到Bootstrap CSS文件:
.navbar-image {
  float: left;
  padding: 10px 5px;
}

修改您的_LoginPartial.cshtml:
using (Html.BeginForm("LogOff", "Account", FormMethod.Post, new { id = "logoutForm", @class = "navbar-right" }))
{

@Html.AntiForgeryToken()

<div class="navbar-image">
    @Html.GravatarImage(User.Identity.GetUserName();, new GravatarOptions { Size = 30, CssClass = "img-circle" })
</div>

<ul class="nav navbar-nav navbar-right">
    <li>
        @Html.ActionLink("Hello " + User.Identity.GetUserName(); + "!", "Manage", "Account", routeValues: null, htmlAttributes: new { title = "Manage" })
    </li>
    <li><a href="javascript:document.getElementById('logoutForm').submit()">Log off</a></li>
</ul>
}

Link,但是如果您花了整整30天的时间,这可能会有些道理:P

10-06 12:47