C# FileContentResult返回一个图像,而不是Null
我想从FileContentResult方法返回一个“默认图像”,而不是null。 基本上,我在整个项目的许多视图中调用下面的方法。 但问题是当没有图像供方法检索时,它返回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
[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?