views:

143

answers:

1
+2  Q: 

MVC question Elmah

Hi Experts,

I am working on a MVC project where I have copied a lot of work from NerdDinner project. In NerdDinner we are returning few views like DinnerNotFound, InvalidOwner if a dinner is not found or if a user is not an owner of the dinner. But In my project want to create an view (CustomException) and use it for all such reasons. So I raise exception and catch them in my basecontrller's OnException event. Then from there I want to render a Custom view after loging it to ELMAH.

But the call to render that view (RedirectToAction("CustomException",ce );) doesnt seem working, it doenst navigate to the action CustomException.

Can someone help me what could be the reason. I have listed all the files here. Also how should I make an entry into global.asax.cs file. codes are given below.

Regards Parminder

ListingExceptions.cs

namespace Listing.Exceptions { public static class ListingExeceptions {

    public static CustomException GetCustomException(Exception ex)
    {
        CustomException ce = new CustomException();
        switch (ex.Message)
        {
            case ItemConstant.INVALID_OWNER:
                ce = new CustomException("Invalid Owner", "OOps you are not owner of this item");
                break;
            case ItemConstant.ITEM_NOT_FOUND:
                ce = new CustomException("Item not Found", "The Item you are looking for couldnt be found");
                break;
            default:
               ce = new CustomException("Error ", "There is an Error.");
               break;
        }

        return ce;

    }


}

}

BaseController.cs

namespace Listing.Controllers { public partial class BaseController : Controller {

    public virtual ActionResult CustomException(CustomException ce)
    {
        return View(ce);   
    }

    protected override void OnException(ExceptionContext filterContext)
    {
        base.OnException(filterContext);

        CustomException ce = ListingExeceptions.GetCustomException(filterContext.Exception);
        ErrorSignal.FromCurrentContext().Raise(filterContext.Exception);
        filterContext.ExceptionHandled = true;

        RedirectToAction("CustomException",ce );
    }
}

}

ListingController.cs

namespace Listing.Controllers

{

public virtual ActionResult Details(long id, string title) {

        Item item = itemRepository.GetItemByID(id);

        if (item == null)

        {

            throw new Exception("ItemNotFound");

        }

        else

        {

            return View("Details", new ListingFormViewModel(item, photoRepository));

        }
    }

}

global.asax.cs

routes.MapRoute( "Exception", // Route name "{controller}/{action}/{ce}", // URL with parameters new { controller = "Base", action = "CustomException", ce = "" } );

+1  A: 

I'm not so sure you can redirect like that within OnException. Adding a filterContext.Result = should work:

protected override void OnException(ExceptionContext filterContext)
{
    base.OnException(filterContext);

    CustomException ce = ListingExeceptions.GetCustomException(filterContext.Exception);
    ErrorSignal.FromCurrentContext().Raise(filterContext.Exception);
    filterContext.ExceptionHandled = true;

    filterContext.Result = RedirectToAction("CustomException",ce );
}
eyesnz
it helped.thanks eyenz
Parminder