-2

filterContext.Result = new JsonResult() in above code(CustomJsonExceptionHandler) does not work properly when I access website from outside the server.

Please see below code, function is throwing an exception. Custom attribute is being used to return the error message to ajax function

controller function

[HttpPost]
[CustomJsonExceptionHandler]
public ActionResult GetList(int month, int year)
{
    throw new ArgumentException("Exception");
    List<Dictionary<string, object>> List = new List<Dictionary<string,object>>()
    return Json(List, JsonRequestBehavior.AllowGet);
}

Custom error attribute

public class CustomJsonExceptionHandler : HandleErrorAttribute
{

    public override void OnException(ExceptionContext filterContext)
    {
        if (filterContext != null && filterContext.Exception != null)
        {

            filterContext.ExceptionHandled = true;
            filterContext.HttpContext.Response.StatusCode = 500;
            filterContext.Result = new JsonResult()
            {
                JsonRequestBehavior = JsonRequestBehavior.AllowGet,
                Data = new
                {
                    errorMessage = filterContext.Exception.Message
                }
            };
        }
    }
}

Please see below code of jquery ajax function

$.fn.GetData = function (month, year) {
    var data = null;
    $.ajax({
        url: '/HomeController/GetList',
        data: { month: month, year: year },
        type: 'POST',
        async: false,
        success: function (Listdata) {
            data = Listdata;
        },
        error: function (xhr) {
            var jsonerror = $.parseJSON(xhr.responseText)
            alert(jsonerror.errorMessage)
        }
    });
    return data;
}

Please Help !!

rashfmnb
  • 9,959
  • 4
  • 33
  • 44
  • What does the exception message say? – ADyson Apr 27 '16 at 15:11
  • In jQuery function xhr.responseText does not come as expected. Hence $.parseJSON throwing an error. Hope that make sense – Niranjan Vesikar Apr 28 '16 at 04:53
  • It does but I don't think anyone can help you unless you tell us exactly what the error says. Just saying "it throws an error" doesn't give us any clues – ADyson Apr 28 '16 at 08:04
  • Hi Dyson, In CustomJsonExceptionHandler class OnException function returns a exception in Json format ({"errorMessage" : "Exception"}). jQuery function GetData accepts this error in error property. This works fine when I access website in localhost. But not working outside the localhost. xhr.responseText gives some weird text please see text in next comment – Niranjan Vesikar Apr 28 '16 at 08:16
  • ([+] xhr.responseText " \r\n \r\n \r\n\r\n 500 - Internal server error.\r\n – Niranjan Vesikar Apr 28 '16 at 08:21
  • serif;color:#FFF;\r\nbackground-color:#555555;}\r\n#content{margin:0 0 0 2%;position:relative;}\r\n.content-container { background:#FFF;width:96%;margin-top:8px;padding:10px;position:relative;}\r\n-->\r\n\r\n\r\n\r\n

    Server Error

    \r\n
    \r\n
    \r\n

    500 - Internal server error.

    \r\n

    There is a problem with the resource you are looking for, and it cannot be displayed.

    \r\n
    \r\n
    \r\n\r\n
    – Niranjan Vesikar Apr 28 '16 at 08:22
  • Ok so that's just the standard HTTP 500 server error. It does not tell you what is going wrong internally. You need to debug on the server side, or log the exception into the Windows event log, and then you should see what the real error is inside the server – ADyson Apr 28 '16 at 08:48
  • I got the solution please find answer in http://stackoverflow.com/questions/5385714/deploying-website-500-internal-server-error – Niranjan Vesikar Apr 28 '16 at 13:03
  • That link tells you a way to get your underlying error message, yes. Did you also fix the problem that was causing the error? If so, you should post the solution here. – ADyson Apr 28 '16 at 14:33

1 Answers1

2

Hi issue is resolved I have added following to web.config

<system.webServer>
    <httpErrors errorMode="Detailed" />
    <asp scriptErrorSentToBrowser="true"/>
  </system.webServer>

please see below link for reference

click here

Community
  • 1
  • 1