private static void RenderAjaxView(HttpApplication httpApplication, HttpStatusCode httpStatusCode,
                                           Exception currentError)
        {
            // Ok. lets check if this content type contains a request for json.
            string errorMessage = httpApplication.Request.ContentType.Contains("json")
                                       ? currentError.Message
                                       : string.Format(
                "An error occured but we are unable to handle the request.ContentType [{0}]. Anyways, the error is: {1}",
                httpApplication.Request.ContentType,
                currentError.Message);


            var errorController   = new FakeErrorController();
            var controllerContext =
                new ControllerContext(httpApplication.Context.Request.RequestContext, errorController);
            var jsonResult = new JsonResult
            {
                JsonRequestBehavior = JsonRequestBehavior.AllowGet,
                Data = new
                {
                    error_message = errorMessage
                }
            };

            jsonResult.ExecuteResult(controllerContext);

            // Lets make sure we set the correct Error Status code :)
            httpApplication.Response.StatusCode = (int)httpStatusCode;
        }
        private static void RenderCustomErrorView(HttpApplication httpApplication, string viewPath,
                                                  HttpStatusCode httpStatusCode,
                                                  Exception currentError)
        {
            try
            {
                // We need to render the view now.
                // This means we need a viewContext ... which requires a controller.
                // So we instantiate a fake controller which does nothing
                // and then work our way to rendering the view.
                var errorController   = new FakeErrorController();
                var controllerContext =
                    new ControllerContext(httpApplication.Context.Request.RequestContext, errorController);
                var view      = new RazorView(controllerContext, viewPath, null, false, null);
                var viewModel = new ErrorViewModel
                {
                    Exception = currentError
                };
                var tempData    = new TempDataDictionary();
                var viewContext = new ViewContext(controllerContext, view,
                                                  new ViewDataDictionary(viewModel), tempData,
                                                  httpApplication.Response.Output);
                view.Render(viewContext, httpApplication.Response.Output);

                // Lets make sure we set the correct Error Status code :)
                httpApplication.Response.StatusCode = (int)httpStatusCode;
            }
            catch (Exception exception)
            {
                // Damn it! Something -really- crap just happened.
                // eg. the path to the redirect might not exist, etc.
                string errorMessage =
                    string.Format(
                        "An error occured while trying to Render the custom error view which you provided, for this HttpStatusCode. ViewPath: {0}; Message: {1}",
                        string.IsNullOrEmpty(viewPath) ? "--no view path was provided!! --" : viewPath,
                        exception.Message);

                RenderFallBackErrorViewBecauseNoneWasProvided(httpApplication,
                                                              HttpStatusCode.InternalServerError,
                                                              new InvalidOperationException(errorMessage, currentError));
            }
        }
Exemplo n.º 3
0
 protected static ControllerContext NewControllerContext(HttpRequestBase request)
 {
     var errorController = new FakeErrorController();
     var controllerContext = new ControllerContext(request.RequestContext, errorController);
     return controllerContext;
 }
        private static void RenderCustomErrorView(HttpApplication httpApplication, string viewPath,
                                                  HttpStatusCode httpStatusCode,
                                                  Exception currentError)
        {
            try
            {
                // We need to render the view now.
                // This means we need a viewContext ... which requires a controller.
                // So we instantiate a fake controller which does nothing
                // and then work our way to rendering the view.
                var errorController = new FakeErrorController();
                var controllerContext =
                    new ControllerContext(httpApplication.Context.Request.RequestContext, errorController);
                var view = new RazorView(controllerContext, viewPath, null, false, null);
                var viewModel = new ErrorViewModel
                                {
                                    Exception = currentError
                                };
                var tempData = new TempDataDictionary();
                var viewContext = new ViewContext(controllerContext, view,
                                                  new ViewDataDictionary(viewModel), tempData,
                                                  httpApplication.Response.Output);
                view.Render(viewContext, httpApplication.Response.Output);

                // Lets make sure we set the correct Error Status code :)
                httpApplication.Response.StatusCode = (int) httpStatusCode;
            }
            catch (Exception exception)
            {
                // Damn it! Something -really- crap just happened. 
                // eg. the path to the redirect might not exist, etc.
                string errorMessage =
                    string.Format(
                        "An error occured while trying to Render the custom error view which you provided, for this HttpStatusCode. ViewPath: {0}; Message: {1}",
                        string.IsNullOrEmpty(viewPath) ? "--no view path was provided!! --" : viewPath,
                        exception.Message);

                RenderFallBackErrorViewBecauseNoneWasProvided(httpApplication,
                                                              HttpStatusCode.InternalServerError,
                                                              new InvalidOperationException(errorMessage, currentError));
            }
        }
        private static void RenderAjaxView(HttpApplication httpApplication, HttpStatusCode httpStatusCode,
                                             Exception currentError)
        {
            // Ok. lets check if this content type contains a request for json.
            string errorMessage = httpApplication.Request.ContentType.Contains("json")
                                       ? currentError.Message
                                       : string.Format(
                                           "An error occured but we are unable to handle the request.ContentType [{0}]. Anyways, the error is: {1}",
                                           httpApplication.Request.ContentType,
                                           currentError.Message);
            

            var errorController = new FakeErrorController();
            var controllerContext =
                new ControllerContext(httpApplication.Context.Request.RequestContext, errorController);
            var jsonResult = new JsonResult
            {
                JsonRequestBehavior = JsonRequestBehavior.AllowGet,
                Data = new
                {
                    error_message = errorMessage
                }
            };
            jsonResult.ExecuteResult(controllerContext);

            // Lets make sure we set the correct Error Status code :)
            httpApplication.Response.StatusCode = (int) httpStatusCode;
        }