2

I want to throw a exception in the ActionFilterAttribute like this:

public override void OnActionExecuting(ActionExecutingContext filterContext) {
    base.OnActionExecuting(filterContext);

    if (!_controller.HasRoleIndex()) {
        throw new Exception(Resources.BackOffice.lblErrorPermission);
    }
}

I want it like this, because I want to execute this code in some ajax request, and when the user dosen't have permissions, send a exception and then showing a message.

I have the code of showing the message in the exception implemented, and it works for exeptions in other places of the aplication, but when an exception is thrown in the OnActionExecuting of the ActionFilterAttribute, a unhandled exception in the application, and what I get in the view is a Server Error.

How can I handle the exception, without redirecting to another page? I only want to throw the exception, no show a 404 error page.

Edit:

To put things in context: I have a web page with some ajax calls, I wanted the ajax calls to have a behavior in case that an error ocurred while the call was made so I have something like this in all my ajaxs:

 $.ajax({
 ...
 error: function (e, o, errorText) { AlertMessages.SetErrorMessageAndShow(errorText); },
 ...
});

This code shows a modal window with a friendly user message with the cause of the error: Session expired. Bad email adress, etc.

I want to use the same code for validation. And with validation I was thinking of use ActionFilterAttribute.

But I can't get how to send and exception from the OnActionExecuting.

Is this possible? or i have to return a specific Json and then in the javascript act acording the json returned?

Albert Cortada
  • 737
  • 3
  • 10
  • 25
  • http://stackoverflow.com/questions/15203918/catch-404-error-in-mvc4 have a look at this – Tushar Gupta Oct 27 '14 at 09:39
  • This answers seems to redirect to another page, and I don't want to navigate to another page. I want the controller to return a exception, not a redirecto to another page. – Albert Cortada Oct 27 '14 at 10:22
  • what does it mean to return an exception? you have to show the user something. that something is usually directing to an error page. – DLeh Oct 27 '14 at 15:44
  • I have edited the question trying to explain why I want to throw an exception. – Albert Cortada Oct 27 '14 at 16:34

0 Answers0