FileContentResult返回图片而不是空值

3
我希望能在FileContentResult结果方法中返回一张“默认图像”,而不是null。基本上,我在项目的许多视图中调用下面的方法。但问题是,当该方法无法检索到图像时会返回null,并导致调用它的每个页面出现错误。如果没有检索到图像,则要使用保存在项目中的图像进行显示。我不想在数据库中保存默认图像。任何帮助将不胜感激...
       [AllowAnonymous]
    public FileContentResult GetLogoImage()
    {

        var logo = _adminPractice.GetAll().FirstOrDefault();
        if (logo != null)
        {
            return new FileContentResult(logo.PracticeLogo, "image/jpeg");
        }
        else
        {
            return null;
        }
    }

你的问题非常简单,因此令人困惑。为什么不能使用默认图像返回FCR? - user1228
1个回答

6
您应该按照以下方式映射文件路径:

[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,所以您需要更改函数的返回类型。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接