1

Can someone help me with this. I am looking to put this code into a try catch. But I can't say how best to implement this:

public JsonResult JsonLogin(LoginModel model, string returnUrl)
        {
            if (ModelState.IsValid)
            {
                if (Membership.ValidateUser(model.UserName, model.Password))
                {
                    FormsAuthentication.SetAuthCookie(model.UserName, model.RememberMe);
                    return Json(new JsonDialog
                    {
                        Redirect = returnUrl,
                        Success = true
                    });
                }
                else
                {
                    ModelState.AddModelError("", "The user name or password provided is incorrect.");
                }
            }
            // If we got this far, something failed
            return Json(new JsonDialog
            {
                Errors = GetErrorsFromModelState(),
                Success = false
            });
        }

Should I put all of the code including the first "if" within the try catch?

Alan2
  • 23,493
  • 79
  • 256
  • 450

1 Answers1

1

I don't think you should have a try catch here at all. What is the exception you would be expecting here.

Instead I'd look at using Elmah and the HandleError attribute on your controller, or base controller if you have one, read below:

How to get ELMAH to work with ASP.NET MVC [HandleError] attribute?

Community
  • 1
  • 1
dove
  • 20,469
  • 14
  • 82
  • 108