Exemple #1
0
        protected void Application_Error(object sender, EventArgs e)
        {
            var app     = (MvcApplication)sender;
            var context = app.Context;
            var ex      = app.Server.GetLastError();

            context.Response.Clear();
            context.ClearError();
            var httpException = ex as HttpException;

            var routeData = new RouteData();

            routeData.Values["controller"] = "error";
            routeData.Values["action"]     = "Index";
            if (httpException != null)
            {
                switch (httpException.GetHttpCode())
                {
                case 403:
                    routeData.Values["action"] = "NoPermission";
                    break;

                case 404:
                    routeData.Values["action"] = "NotFound";
                    break;

                default:
                    routeData.Values["action"] = "Index";
                    break;
                }
            }
            IController controller = new errorController();

            controller.Execute(new RequestContext(new HttpContextWrapper(context), routeData));
        }
Exemple #2
0
        /*
         * protected void Application_EndRequest()
         * {
         *  if (Context.Response.StatusCode == 404)
         *  {
         *      Response.Clear();
         *
         *      var rd = new RouteData();
         *      rd.DataTokens["area"] = ""; // In case controller is in another area
         *      rd.Values["controller"] = "error";
         *      rd.Values["action"] = "notfound";
         *
         *      IController c = new errorController();
         *      c.Execute(new RequestContext(new HttpContextWrapper(Context), rd));
         *  }
         * }
         */

        protected void Application_Error(object sender, EventArgs e)
        {
            Exception exception = Server.GetLastError();

            Response.Clear();

            var httpException = exception as HttpException;

            var         routeData       = new RouteData();
            IController errorController = new errorController();

            if (httpException != null)
            {
                switch (httpException.GetHttpCode())
                {
                case 401:
                    routeData.Values["controller"] = "Error";
                    routeData.Values["action"]     = "NoAuth";
                    break;

                case 404:
                    var      area        = Request.RequestContext.RouteData.DataTokens["area"] as string;
                    string[] host        = Request.Headers["Host"].Split('.');
                    string   hostSegment = "";
                    if (string.IsNullOrEmpty(area) && Utility.IsOnSubdomain(host, out hostSegment))
                    {
                        errorController = new liquidErrorController();
                        routeData.Values["controller"] = "liquidError";
                        routeData.Values["action"]     = "NotFound";
                    }
                    else
                    {
                        routeData.Values["controller"] = "Error";
                        routeData.Values["action"]     = "NotFound";
                        routeData.Values["url"]        = Request.Url.OriginalString;
                    }
                    break;

                default:
                    routeData.Values["controller"] = "Error";
                    routeData.Values["action"]     = "Index";
                    break;
                }
            }

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

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

            // Call target Controller and pass the routeData.
            errorController.Execute(new RequestContext(
                                        new HttpContextWrapper(Context), routeData));
        }