Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/77.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# FileContentResult返回一个图像,而不是Null_C#_Html_Asp.net Mvc 4 - Fatal编程技术网

C# FileContentResult返回一个图像,而不是Null

C# FileContentResult返回一个图像,而不是Null,c#,html,asp.net-mvc-4,C#,Html,Asp.net Mvc 4,我想从FileContentResult方法返回一个“默认图像”,而不是null。 基本上,我在整个项目的许多视图中调用下面的方法。 但问题是当没有图像供方法检索时,它返回null并在调用它的每个页面上导致错误。 如果没有检索到图像,我想使用保存在项目中的图像进行显示。 我不想在数据库中保存默认图像 感谢您的帮助 [AllowAnonymous] public FileContentResult GetLogoImage() { var logo

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

您应该按如下方式映射文件的路径:

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

您的问题非常简单,难以解决,因此容易混淆。为什么不能返回带有默认图像的FCR?