views:

118

answers:

2

I have a page that contains multiple div's. Action on one div changes the content of other div. Each div loads a partial view from controller. For example- a div for "Search" and another div for "SearchResult". "Search" div contains couple of fields and a button. On click of this button a controller method is invoked asynchronously using Ajax. This controller method can either return a "Search Result" partial view or throw an exception.

On successful method execution, my Ajax callback will update the content of "SearchResult" div using the partial view data returned by the controllers method.

For all unhandled exceptions, I am using Elmah. Based on the above design, I would want Elmah to log an error and redirect to a Partial View. I have tried customerrors and it works for the View. One option is to remove the Error.aspx and set the defaultHandler to invoke another method (say MyCustomError) of controller which can return a Custom error as Partial View. How do I get the original exception details in this "MyCustomError" method or "MyCustomError" partial view ?

EDIT: For Elmah, I am using the solution by Elmah author - http://stackoverflow.com/questions/766610/how-to-get-elmah-to-work-with-asp-net-mvc-handleerror-attribute/779961#779961

+1  A: 

Maybe pass the exception as the model to the partial view?

Update

public ActionResult DoSomething()
{

    try
    {
        ...
    } 
    catch(Exception e)
    {
        // send the error to Elmah
        ErrorSignal.FromCurrentContext().Raise(e);
        // pass it to the error display partial view.
        return View("ErrorDisplayControl",e);
    }

    return View();

}

If you include this in your main view using RenderAction() as opposed to RenderPartial(), you can log the error and then return the correct view.

Also, you're probably aware that you should present a more friendly error to the user instead of the real exception message. Not only are exception messages confusing to users, providing that sort of raw data to the client introduces some security concerns.

David Lively
The problem is that the exception should first go to Elmah so it is logged.
byte
See my update..
David Lively
Thanks David, I currently have similar code like what you have given above minus the ErrorSignal*** line. If you see my edit above, I have shown the Elmah solution that I am using. This helps simply write HandleError attribute instead of ErrorSignal*** everywhere in code. But the solution that you have provided and which I have already in my app doesn't fit well with the HandleError filter solution provided in the above link
byte
Upvoted your solution
byte
+1  A: 

The TempData property works well for this sort of thing. Any information you put into TempData will stay there through the following request. So if you put the error information there when it occurs, and then your client-side code creates a new ajax request to the error page, the error controller action or partial view can retrieve the error information from TempData.

StriplingWarrior
By "redirect to partial view" i meant that once Elmah logs the exception, my custom error page should be returned. Something on the lines of defaultRedirect attribute of customErrors in web.config or "View" argument of HandleError attribute.In my case, I would want a partial view to be returned.
byte