嗨,我是编码新手,目前正在构建 MVC,但出现 CS0161 错误,我似乎无法修复它。任何帮助表示赞赏
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
CS0161 是您在编程中遇到的最明显的错误。它说
A method that returns a value must have a return statement in all code paths
这意味着,在英语中,如果您的方法 return 是某种东西,那么每个代码路径都必须 return 那种东西
所以这里出了什么问题:
public IActionResult Details(int id)
{
var funko = _funkoRepository.GetFunkoById(id);
if(funko == null)
return NotFound();
}
如果 funko
是 NOT null 那么什么都不会 returned。通过 returning 低于 if
条件的内容来修复它。通常这可能看起来像:
public IActionResult Details(int id)
{
var funko = _funkoRepository.GetFunkoById(id);
if(funko == null)
return NotFound();
// we found a funko!
return Ok(funko);
}
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
CS0161 是您在编程中遇到的最明显的错误。它说
A method that returns a value must have a return statement in all code paths
这意味着,在英语中,如果您的方法 return 是某种东西,那么每个代码路径都必须 return 那种东西
所以这里出了什么问题:
public IActionResult Details(int id)
{
var funko = _funkoRepository.GetFunkoById(id);
if(funko == null)
return NotFound();
}
如果 funko
是 NOT null 那么什么都不会 returned。通过 returning 低于 if
条件的内容来修复它。通常这可能看起来像:
public IActionResult Details(int id)
{
var funko = _funkoRepository.GetFunkoById(id);
if(funko == null)
return NotFound();
// we found a funko!
return Ok(funko);
}