【发布时间】:2015-10-29 14:47:48
【问题描述】:
我在 layout.cshtml 页面中有一个用户个人资料图片,该图片显示在整个页面中。现在的问题是图像显示在母版页中,但是在导航到子页面时它没有显示。请帮帮我。
登录控制器显示图片:
public FileContentResult DisplayImage(int userId)
{
byte[] imageByteData = null;
try
{
using (var db = new AdminDb())
{
var userImage = (from usr in db.User.AsNoTracking()
where usr.UserId == userId
select usr.UserImage).FirstOrDefault();
if (!string.IsNullOrEmpty(userImage))
{
imageByteData = System.IO.File.ReadAllBytes(userImage);
return File(imageByteData, "image/jpeg");
}
else
{
imageByteData = System.IO.File.ReadAllBytes(Server.MapPath(Url.Content("~/Content/UserProfileImages/defaultimage.png")));
return File(imageByteData, "image/jpeg");
}
}
}
catch (Exception)
{
return File(imageByteData, "image/jpeg");
}
}
用于保存路径的用户控制器。(在此我将图像保存到外部文件夹并将路径保存在数据库中)
if (file.ContentLength > 0)
{
var setting = db.Setting.AsNoTracking().Where(x => x.Code == OtherConstants.UserPhotoPath)
.Select(x => new
{
SettingId = x.SettingId,
SchemaDefinitionId = x.SchemaDefinitionId,
Value = x.Value
}).FirstOrDefault();
string path = setting.Value;
//Getting the path + the image name
var paths = Path.Combine(path + fileName);
user.UserImage = paths;
//Checking whether the uploaded file is already exists and if not exists save the details
if (!System.IO.File.Exists(paths))
{
file.SaveAs(paths);
}
我的 Layout.cshtml 页面(在哪里使用动作控制器)
<img id="imgLogin" class="lblLogin UserProfileDeatils" src="@Url.Action("DisplayImage", "Login", new { userId = Assyst.PanERP.Common.AppSession.Common.UserID })" />
</div>
【问题讨论】:
-
您的子页面是否使用相同的布局页面?
-
你能运行 fiddler 看看是否有调用 API ~/Login/DisplayImage?UserId=> 失败或返回 HttpResponse 状态码为 200?
-
我检查时发现只有一次布局被调用。
标签: c# asp.net-mvc asp.net-mvc-4