Reputation: 13
[HttpPost]
public IActionResult LoginPost(string Username, string Password)
{
User user = dbcontext.User.Where(m => m.Username == Username).FirstOrDefault();
if (user != null && user.IsPWValid(Password))
{
HttpContext.Session.SetString("UserId", user.Id);
TempData["Error"] = "";
return RedirectToAction("Index", "Products");
}
else
{
TempData["Error"] = "Try again";
return RedirectToAction("Login", "Home");
}
}
I want to put exception in this method in ASP.NeET Entity Framework. How to put exception?
Upvotes: 0
Views: 67
Reputation: 1265
Use a try-catch to catch an exception. Perhaps, like:
public IActionResult LoginPost(string Username, string Password)
{
User user = dbcontext.User.Where(m => m.Username == Username).FirstOrDefault();
if (user != null && user.IsPWValid(Password))
{
try
{
HttpContext.Session.SetString("UserId", user.Id);
TempData["Error"] = "";
return RedirectToAction("Index", "Products");
}
catch(Exception e)
{
TempData["Error"] = "Exception occured: " + e;
return RedirectToAction("Login", "Home");
}
}
else
{
TempData["Error"] = "Try again";
return RedirectToAction("Login", "Home");
}
}
Hope it helps.
Upvotes: 1