我想显示在我的应用程序的所有页面上登录的用户的图像。我已成功为用户详细信息制作了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(实体框架)。
答案 0 :(得分:-1)
你必须为此写一个助手类我告诉你如何为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