开发者

Custom Error Page Being Sent With Ajax Response in ASP.NET MVC 3

开发者 https://www.devze.com 2023-03-29 17:25 出处:网络
Why would the custom error page be sent with the ajax response below when an error occurs? Response {\"Errors\":[\"An error has occurred and we have been notified.We are sorry for the inconvenience.

Why would the custom error page be sent with the ajax response below when an error occurs?

Response

{"Errors":["An error has occurred and we have been notified.  We are sorry for the inconvenience."]}<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8" />
    <title>Error</title>

Web.Config

 <customErrors defaultRedirect="Error" mode="On"></customErrors>

BaseController.cs

public class BaseController : Controller
    {
        protected override void OnException(ExceptionContext filterContext)
        {
            if (filterContext.HttpContext.Request.IsAjaxRequest())
            {
                var response = filterContext.HttpContext.Response;

                var validatorModel = new ValidatorModel();

                if (filterContext.Exception is AriesException && !((AriesException)filterContext.Exception).Visible &&am开发者_高级运维p; filterContext.HttpContext.IsCustomErrorEnabled)
                {
                    validatorModel.Errors.Add(this.Resource("UnknownError"));
                }
                else
                {
                    validatorModel.Errors.Add(filterContext.Exception.Message);
                }

                response.Clear();
                response.StatusCode = (int)System.Net.HttpStatusCode.InternalServerError;
                response.Write(validatorModel.ToJson());
                response.ContentType = "application/json";
                response.TrySkipIisCustomErrors = true;
                filterContext.ExceptionHandled = true;
                System.Web.HttpContext.Current.ApplicationInstance.CompleteRequest();
            }
            else if (filterContext.HttpContext.IsCustomErrorEnabled)
            {
                filterContext.ExceptionHandled = true;
            }

            if(filterContext.ExceptionHandled)
            {
                SiteLogger.Write(filterContext.Exception);
            }
        }


   }


In case anyone is still having this issue, I found a slightly cleaner solution:

if (!filterContext.HttpContext.Request.IsAjaxRequest())
{
        //non-ajax exception handling code here
}
else
{
        filterContext.Result = new HttpStatusCodeResult(500);
        filterContext.ExceptionHandled = true;
}


dsomuah's solution is nice, but has to be added to each controller that serves Ajax requests. We took it a step further by globally registering the following action filter:

public class HandleAjaxErrorAttribute : HandleErrorAttribute
{
    public override void OnException(ExceptionContext filterContext)
    {
        if (filterContext.RequestContext.HttpContext.Request.IsAjaxRequest())
        {
            filterContext.ExceptionHandled = true;
            filterContext.HttpContext.Response.StatusCode = (int)HttpStatusCode.InternalServerError;
            filterContext.HttpContext.Response.StatusDescription = filterContext.Exception.Message;
        }
    }
}


I added response.End(); and it worked. Is there a better way?

0

精彩评论

暂无评论...
验证码 换一张
取 消