I have a standard Edit action in Asp.Net MVC 5 and I want to avoid throwing the unhandled exception when is made a get request without the id like ~/food/edit, so I did this.
public ActionResult Edit(int id = 0)
{
if (id == 0)
{
return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
}
string result = _foodAppService.GetById(id);
FoodVm food = string.IsNullOrEmpty(result)
? null
: JsonConvert.DeserializeObject<FoodVm>(result);
if (food == null)
{
return RedirectToAction("Index");
}
return View(food);
}
My question is: Is it a good practice to handled it in this way or there are more suitable strategies ?
I'm new to this asking question thing, if a should I ask in another way, just let me know, thank you for your time.