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?