Example #1
0
 public ActionResult NotFound()
 {
     var errorController = new ErrorController();
     if (errorController != null)
     {
         return errorController.Http404(new Exception("Page Not Found"));
     }
     return null;
 }
Example #2
0
        protected void Application_Error(object sender, EventArgs e)
        {
            Exception exception = Server.GetLastError();

            Response.Clear();

            HttpException httpException = exception as HttpException;

            RouteData routeData = new RouteData();
            routeData.DataTokens.Add("namespaces", new[] { typeof(ErrorController).Namespace });
            routeData.DataTokens.Add("area", "Error");
            routeData.Values.Add("controller", "Error");
            
            if (httpException == null)
            {
                routeData.Values.Add("action", "Exception");
            }
            else //It's an Http Exception, Let's handle it.
            {
                switch (httpException.GetHttpCode())
                {
                    case 401:
                        // Unauthorized.
                        routeData.Values.Add("action", "Http401");
                        break;
                    case 403:
                        // Forbidden.
                        routeData.Values.Add("action", "Http403");
                        break;
                    case 404:
                        // Page not found.
                        routeData.Values.Add("action", "Http404");
                        break;
                    case 500:
                        // Server error.
                        routeData.Values.Add("action", "Http500");
                        break;

                    // Here you can handle Views to other error codes.
                    // I choose a General error template  
                    default:
                        routeData.Values.Add("action", "General");
                        break;
                }
            }

            // Pass exception details to the target error View.
            routeData.Values.Add("exception", exception);

            // Clear the error on server.
            Server.ClearError();

            // Avoid IIS7 getting in the middle
            Response.TrySkipIisCustomErrors = true;

            // If it is an Ajax request, we should respond with Json data, otherwise redirect
            if (new HttpRequestWrapper(Request).IsAjaxRequest())
            {
                string jsonResult = string.Empty;
                if (httpException == null)
                {
                    jsonResult = Json.Encode(new { error = new { type = "Exception", message = exception.GetFullMessage(true) } });
                }
                else
                {
                    jsonResult = Json.Encode(new { error = new { type = "Http", statuscode = httpException.GetHttpCode(), message = exception.GetFullMessage(true) } });
                }
                Response.Write(jsonResult);
            }
            else
            {
                // Call target Controller and pass the routeData.
                IController errorController = new ErrorController();
                errorController.Execute(new RequestContext(
                     new HttpContextWrapper(Context), routeData));
            }
        }
        protected void HandleInvalidAuthRequest(AuthorizationContext filterContext)
        {
            // auth failed, redirect to login page
            var request = filterContext.HttpContext.Request;
            string redirectUrl = (request.Url != null) ? filterContext.HttpContext.Request.Url.AbsoluteUri.ToString() : string.Empty;

            var errorController = new ErrorController();
            if (errorController != null)
            {
                filterContext.Result = errorController.Http403(new Exception("Not Authorized"));
                return;
            }
            filterContext.Result = new HttpUnauthorizedResult();
        }