Display server side Exception Message on DataTables.NET

試著忘記壹切 提交于 2019-12-12 04:16:51

问题


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.

来源:https://stackoverflow.com/questions/33940524/display-server-side-exception-message-on-datatables-net

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!