namespace Funko626.Controllers
{
public class FunkoController : Controller
{
private readonly IFunkoRepository _funkoRepository;
private readonly IBrandRepository _brandRepository;
public FunkoController(IFunkoRepository funkoRepository, IBrandRepository brandRepository)
{
_funkoRepository = funkoRepository;
_brandRepository = brandRepository;
}
public ViewResult List()
{
FunkoListViewModel funkoListViewModel = new()
{
Funkos = _funkoRepository.AllFunkos,
CurrentBrand = " "
};
return View(funkoListViewModel);
}
//I think it's this part of the code with 'int id' but cant be sure.
public IActionResult Details(int id)
{
var funko = _funkoRepository.GetFunkoById(id);
if(funko == null)
return NotFound();
}
}
}
Severity Code Description Project File Line Suppression State
Error CS0161 ‘FunkoController.Details(int)’: not all code paths return
a
value Funko626 C:\Users\bidde\source\repos\Funko626\Controllers\FunkoController.cs 29 Active
>Solution :
CS0161 is about the most obvious error you’ll get in programming. It says
A method that returns a value must have a return statement in all code paths
Which means, in English, that if your method returns something then every code path must return that thing
So what’s wrong here:
public IActionResult Details(int id)
{
var funko = _funkoRepository.GetFunkoById(id);
if(funko == null)
return NotFound();
}
if funko is NOT null then nothing is returned. Fix it by returning something below the if condition. Usually this might look something like:
public IActionResult Details(int id)
{
var funko = _funkoRepository.GetFunkoById(id);
if(funko == null)
return NotFound();
// we found a funko!
return Ok(funko);
}