Skip to content

Instantly share code, notes, and snippets.

var path = HttpContext.Current.Request.Path;
HttpContext.Current.RewritePath("~/Error/ManualErrors");
HttpContext.Current.Session["statusCode"] = statusCode;
IHttpHandler handler = new MvcHttpHandler();
handler.ProcessRequest(HttpContext.Current);
HttpContext.Current.RewritePath(path);
HttpContext.Current.Server.ClearError();
HttpContext.Current.Session["exception"] = exception;
var url = "~/Error/ManualErrors?statusCode=" + statusCode;
HttpContext.Current.Server.TransferRequest(url, false, "GET", null);
var routeData = new RouteData();
routeData.Values.Add("controller", ErrorControllerRouteName);
routeData.Values.Add("action", actionName);
var requestContext = new RequestContext(
new HttpContextWrapper(HttpContext.Current),
routeData);
var controllerFactory = ControllerBuilder.Current.GetControllerFactory();
var viewResult = new ViewResult {ViewName = "ManualErrors"};
// Setting of values on its ViewBag here.
viewResult.ExecuteResult(controllerContext);
HttpContext.Current.Server.ClearError();
var routeData = new RouteData();
routeData.Values.Add("controller", ErrorControllerRouteName);
var controllerContext = new ControllerContext(
new HttpContextWrapper(HttpContext.Current),
routeData,
new FakeController());
HttpContext.Current.Response.ClearHeaders();
HttpContext.Current.Response.ClearContent();
HttpContext.Current.Response.StatusCode = statusCode;
HttpContext.Current.Server.ClearError();
var httpException = exception as HttpException;
...
var statusCode = httpException.GetHttpCode();
var exception = HttpContext.Current.Server.GetLastError();
<system.web>
<!-- Additional configuration elided. -->
<httpModules>
<add
name="ErrorHandlerHttpModule"
type="MvcErrorHandling.MvcHelpers.ErrorHandlerHttpModule, MvcErrorHandling" />
</httpModules>
</system.web>
<system.webServer>