0

I am developing a MVC application which has handles authorization and login information at a base controller class overriding OnActionExecuting event.

At AJAX calls when an exception arises I can handle this via attributes and display error messages with on custom model window.

My custom attribute is as follows :

public class JsonExceptionFilterAttribute : FilterAttribute, IExceptionFilter
{
    public void OnException(ExceptionContext filterContext)
    {
        if (filterContext.RequestContext.HttpContext.Request.IsAjaxRequest())
        {
            filterContext.HttpContext.Response.StatusCode = 500;
            filterContext.ExceptionHandled = true;

            string msg = HttpUtility.HtmlDecode(filterContext.Exception.Message);
            if (filterContext.Exception.GetType() == Type.GetType("System.UnauthorizedAccessException"))
            {
                msg = "Unauthorized access";
            }

            filterContext.Result = new JsonResult
            {
                Data = new
                {
                    errorMessage = msg
                },
                JsonRequestBehavior = JsonRequestBehavior.AllowGet
            };
        }
    }
}

But when an Exception occurs while using DataTables.Net the only mesage I get is the DataTables.NET's own error mesaages saying "... please see http://datatables.net/tn/7"

But I want to display my own Exception message as I do in other AJAX calls. Basically I want to display the error message I provide in my custom attribute response.

I have Googled it, advising to use "fnServerData" but I can not find my Exception message in any of the parameters (sSource, aoData, fnCallback, oSettings) I get by this event handler.

How can I possibly get the Exception message that my base controller returns and display it?

Regards.

P.S. : Handling the Exception in the Action and returning it does not apply here, because I do not fall to Action method at all.

2
  • The error might be because datatables is expecting some return values ie iTotalRecords, iTotalDisplayRecords, aaData, even if they are null or zero values. If you add these to the returned json, you should be able to access your custom message using fnServerData. Commented Nov 26, 2015 at 15:40
  • Good point I will try that. Commented Nov 26, 2015 at 18:50

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.