我想从 FileContentResult 结果方法返回一个“默认图像”,而不是 null。 基本上,我在整个项目的多个 View 中调用了以下方法。 但问题是,当检索方法没有 Image 时,它返回 null 并在调用它的每个页面上导致错误。 如果没有检索到图像,我想使用保存在项目中的图像进行显示。 我不想在数据库中保存默认图像。
感谢任何帮助...
[AllowAnonymous]
public FileContentResult GetLogoImage()
{
var logo = _adminPractice.GetAll().FirstOrDefault();
if (logo != null)
{
return new FileContentResult(logo.PracticeLogo, "image/jpeg");
}
else
{
return null;
}
}
最佳答案
您应该将路径映射到文件,如下所示:
[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");
}
}
两种类型的结果都派生自 FileResult,因此您需要更改函数的返回类型。
关于c# - FileContentResult 返回一个图像而不是 Null,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18213401/