mke*_*ell 3 c# html5 asp.net-mvc-4
我想从FileContentResult结果方法返回"默认图像",而不是null.基本上我在整个项目中的许多视图中都调用了下面的方法.但问题是当没有Image的方法来检索它时返回null并在每个被调用的页面上引起错误.如果没有检索到图像,我想使用项目中保存的图像进行显示.我不想在数据库中保存默认图像.
任何帮助表示赞赏......
[AllowAnonymous]
public FileContentResult GetLogoImage()
{
var logo = _adminPractice.GetAll().FirstOrDefault();
if (logo != null)
{
return new FileContentResult(logo.PracticeLogo, "image/jpeg");
}
else
{
return null;
}
}
Run Code Online (Sandbox Code Playgroud)
您应该将路径映射到该文件,如下所示:
[AllowAnonymous]
public FileResult GetLogoImage()
{
var logo = _adminPractice.GetAll().FirstOrDefault();
if (logo != null)
{
return new FileContentResult(logo.PracticeLogo, "image/jpeg");
}
else
{
return new FilePathResult(HttpContext.Server.MapPath("~/Content/images/practicelogo.jpeg"), "image/jpeg");
}
}
Run Code Online (Sandbox Code Playgroud)
两种类型的结果都来自FileResult,因此您需要更改函数的返回类型.
| 归档时间: |
|
| 查看次数: |
2692 次 |
| 最近记录: |