3

我想从 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;
        }
    }
4

1 回答 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,因此您需要更改函数的返回类型。

于 2013-08-13T16:07:27.957 回答