Display server side Exception Message on DataTable

2019-09-11 12:25发布

问题:

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.