コード例 #1
0
        private static IActionResult GetWebErrorResult(ExceptionContext context)
        {
            var statusCode = GetStatusCodeFromException(context.Exception);

            if (context.HttpContext.Request.IsAjaxRequest() && context.HttpContext.Request.IsJsonRequest())
            {
                var model = new AjaxErrorModel {
                    Message = context.Exception.Message
                };
                var responseJson = model.ToJson();
                return(new ContentResult
                {
                    Content = responseJson,
                    ContentType = "application/json",
                    StatusCode = statusCode
                });
            }

            return(new ViewResult
            {
                ViewName = "~/Views/Error/Details.cshtml",
                ViewData = new ViewDataDictionary <Models.Error.DetailsModel>(new EmptyModelMetadataProvider(), new ModelStateDictionary())
                {
                    Model = new Models.Error.DetailsModel
                    {
                        StatusCode = statusCode,
                        Exception = context.Exception
                    }
                }
            });
        }
コード例 #2
0
        public override void OnActionExecuting(ActionExecutingContext actionContext)
        {
            var request = actionContext.HttpContext.Request;

            if (!request.IsAjaxRequest() || (_jsonRequestBehavior == JsonRequestBehavior.DenyGet && string.Compare(request.HttpMethod, "GET", StringComparison.OrdinalIgnoreCase) == 0))
            {
                actionContext.HttpContext.Response.Clear();
                actionContext.HttpContext.Response.StatusCode = (int)HttpStatusCode.NotAcceptable;
                AjaxErrorModel ajaxErrorModel = new AjaxErrorModel
                {
                    Error = Properties.ValidationResources.AjaxNotAcceptable
                };

                actionContext.Result = new JsonResult
                {
                    Data = ajaxErrorModel,
                    JsonRequestBehavior = JsonRequestBehavior.AllowGet,
                    ContentEncoding     = System.Text.Encoding.UTF8,
                    ContentType         = Constants.JsonContentType,
                };
            }
        }