views:

146

answers:

1

I'm working on an ASP.NET MVC application and just getting to error handling. Help me solve the issue of getting the error message back to the user.

I have a controller:

    [AcceptVerbs(HttpVerbs.Get)]
    public ActionResult Show(string callNumber)
    {
        ServiceCallService sc = new ServiceCallService();
        return View(sc.GetServiceCallByCallNumber("", callNumber));
    }

a Service:

    public ServiceCall GetServiceCallByCallNumber(string custID, string callNumber)
    {
        ServiceCall sc = new ServiceCall();
        sc = _serviceCallRepository.GetServiceCallByCallNumber(custID, callNumber);
        return sc;
    }

a Repository:

    public ServiceCall GetServiceCallByCallNumber(string custID, string callNumber)
    {
        ServiceCall sc = new ServiceCall();

        try
        {
            LoginToDB();
            sc.CallNumber = "123";
        }
        catch (Exception e)
        {
            logger.error(Server.GetLastError());
        }
        finally
        {
            LogoutFromDB();
        }
        return sc;
    }

Let's say there is a problem in the LoginToDB() method. I am logging the error but how do I get the error message back to the controller and then to the view.

Thanks.

+3  A: 

The easiest way are:-

1) To use the build in model validation in ASP.NET MVC Release Canditate 1 (download from asp.net/mvc).

2) Re-throw the exception and catch it in your controller action then pass a nice customized error message to the View to render to the user - pass it using ViewData["error"] or something similar.