Le migliori pratiche per lanciare l'eccezione se nessuna voce trovata nel db?ASP.NET MVC: dove vengono generate le eccezioni?
// CONTROLLER
public ActionResult Edit(int categoryId, int id)
{
Product target = Products.GetById(id);
if (target == null) throw new HttpException(404, "Product not found");
return View("Edit", target);
}
// REPOSITORY
public Product GetById(int id)
{
return context.Products.FirstOrDefault(x => x.productId == id);
}
o
// CONTROLLER
public ActionResult Edit(int categoryId, int id)
{
return View("Edit", Products.GetById(id));
}
// REPOSITORY
public Product GetById(int id)
{
Product target = context.Products.FirstOrDefault(x => x.productId == id);
if (target == null) throw new HttpException(404, "Product not found with given id");
return target;
}
Ma poi devo creare eccezioni personalizzate :(? – ebb
Sì. E questo è quello che dovresti * fare. – blockhead
Creerò solo un'eccezione personalizzata chiamato "NotFoundException" quindi. Grazie per la risposta :) – ebb