internal static void ExecuteLocalRequestAndCaptureResponse(string path, TextWriter writer, ErrorFormatterGenerator errorFormatterGenerator) { HttpRequest request = new HttpRequest(VirtualPath.CreateAbsolute(path), string.Empty); HttpResponse response = new HttpResponse(writer); HttpContext context = new HttpContext(request, response); HttpApplication applicationInstance = HttpApplicationFactory.GetApplicationInstance(context) as HttpApplication; context.ApplicationInstance = applicationInstance; try { context.Server.Execute(path); } catch (HttpException exception) { if (errorFormatterGenerator != null) { context.Response.SetOverrideErrorFormatter(errorFormatterGenerator.GetErrorFormatter(exception)); } context.Response.ReportRuntimeError(exception, false, true); } finally { if (applicationInstance != null) { context.ApplicationInstance = null; HttpApplicationFactory.RecycleApplicationInstance(applicationInstance); } } }