Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Hi, I am new to coding and currently building an MVC and I have a CS0161 ERROR and I cant seem to fix it. Any help appreciated

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 :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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);
}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading