Reputation: 731
I want to return a "default image", instead of null from a FileContentResult result method. Basically I'm calling the below method in a number of views throughout my project. But the problem is when there is no Image for the method to retrieve it returns null and causes an error on each page it's being called on. I want to use an image saved in the project to be displayed if there is no image retrieved. I do not want to save a default image in the database.
Any help is appreciated...
[AllowAnonymous]
public FileContentResult GetLogoImage()
{
var logo = _adminPractice.GetAll().FirstOrDefault();
if (logo != null)
{
return new FileContentResult(logo.PracticeLogo, "image/jpeg");
}
else
{
return null;
}
}
Upvotes: 3
Views: 3233
Reputation: 11252
You should map a path to the file as follows:
[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");
}
}
Both types of results derive from FileResult so you need to change the return type of your function.
Upvotes: 6