开发者

Elmah and partial view

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 cli开发者_StackOverflowck 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 - How to get ELMAH to work with ASP.NET MVC [HandleError] attribute?


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.


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜