コード例 #1
0
		protected void Application_Error(object sender, EventArgs eventArgs)
		{
			var httpException = this.Server.GetLastError() as HttpException ??
			                    new HttpException((int) HttpStatusCode.InternalServerError, 
									"Unexpected Application Error", this.Server.GetLastError());

			/* TODO: Specific controller actions dependent on http status code ?
			var httpStatusCode = (HttpStatusCode) httpException.GetHttpCode();
			var routeAction = "Index";

			switch (httpStatusCode)
			{
				case HttpStatusCode.BadRequest:		routeAction = "BadRequest"; break;
				case HttpStatusCode.Unauthorized:	routeAction = "Unauthorised"; break;
				case HttpStatusCode.Forbidden:		routeAction = "Forbidden"; break;
				case HttpStatusCode.NotFound:		routeAction = "NotFound"; break;
				case HttpStatusCode.RequestTimeout:	routeAction = "RequestTimeout"; break;
				case HttpStatusCode.Conflict:		routeAction = "Conflict"; break;
				case HttpStatusCode.NotImplemented:	routeAction = "NotImplemented"; break;
				default: routeAction = "InternalServerError"; break;
			}*/

			this.Context.ClearError();
			this.Context.Response.Clear();
			this.Context.Response.TrySkipIisCustomErrors = true;
			this.Context.Response.StatusCode = httpException.GetHttpCode();
			
			var routeData = new RouteData();

			routeData.Values["controller"] = "Error";
			routeData.Values["action"] = "Index"; // TODO: routeAction; ?
			routeData.Values["exception"] = httpException;

			IController errorController = new ErrorController();

			errorController.Execute(new RequestContext(new HttpContextWrapper(this.Context), routeData));
        }
コード例 #2
0
        protected void Application_EndRequest(Object sender,
                                                   EventArgs e)
        {
            HttpContext context = HttpContext.Current;
            if (context.Response.Status.Substring(0, 3).Equals("401"))
            {
                this.Context.ClearError();
                this.Context.Response.Clear();
                this.Context.Response.TrySkipIisCustomErrors = true;
                //this.Context.Response.StatusCode = 200; // Note this makes the browser think it is already authenticated

                var routeData = new RouteData();

                routeData.Values["controller"] = "Error";
                routeData.Values["action"] = "Index";
                routeData.Values["exception"] = new HttpException(401, "You are not authorised to access this application.");

                IController errorController = new ErrorController();

                errorController.Execute(new RequestContext(new HttpContextWrapper(this.Context), routeData));
            }
        }