private void EndRequestHandler(object sender, EventArgs e) { // DevDiv 100198: Send error response from EndRequest so Page and Application error handlers still fire on async posts // DevDiv 118737: Call Response.Clear as well as Response.ClearHeaders to force status code reset in integrated mode and // to ensure there are no errant headers such as a caching policy. Do not call Response.End or app.CompleteRequest as they // are pointless from the EndRequest event. HttpApplication app = (HttpApplication)sender; HttpContext context = app.Context; object o = context.Items[PageRequestManager.AsyncPostBackErrorKey]; if ((o != null) && ((bool)o == true)) { context.ClearError(); context.Response.ClearHeaders(); context.Response.Clear(); context.Response.Cache.SetCacheability(HttpCacheability.NoCache); context.Response.ContentType = "text/plain"; string errorMessage = (string)context.Items[PageRequestManager.AsyncPostBackErrorMessageKey]; o = context.Items[PageRequestManager.AsyncPostBackErrorHttpCodeKey]; // o should definitely be an int, but user code could overwrite it int httpCode = (o is int) ? (int)o : 500; PageRequestManager.EncodeString(context.Response.Output, PageRequestManager.ErrorToken, httpCode.ToString(CultureInfo.InvariantCulture), errorMessage); } }
private static void HttpResponse_Redirecting(object sender, EventArgs e) { HttpResponse response = (HttpResponse)sender; HttpContext context = response.Context; // Is in async postback, get status code and check for 302 if (PageRequestManager.IsAsyncPostBackRequest(new HttpRequestWrapper(context.Request))) { // Save the redirect location and other data before we clear it string redirectLocation = response.RedirectLocation; List <HttpCookie> cookies = new List <HttpCookie>(response.Cookies.Count); for (int i = 0; i < response.Cookies.Count; i++) { cookies.Add(response.Cookies[i]); } // Clear the entire response and send a custom response that the client script can process response.ClearContent(); response.ClearHeaders(); for (int i = 0; i < cookies.Count; i++) { response.AppendCookie(cookies[i]); } response.Cache.SetCacheability(HttpCacheability.NoCache); response.ContentType = "text/plain"; // DevDiv#961281 // Allow apps to access to the redirect location context.Items[PageRequestManager.AsyncPostBackRedirectLocationKey] = redirectLocation; // Preserve redirected state: TFS#882879 response.IsRequestBeingRedirected = true; PageRequestManager.EncodeString(response.Output, PageRequestManager.UpdatePanelVersionToken, String.Empty, PageRequestManager.UpdatePanelVersionNumber); // url encode the location in a way that javascript unescape() will be able to reverse redirectLocation = String.Join(" ", redirectLocation.Split(' ').Select(part => HttpUtility.UrlEncode(part))); PageRequestManager.EncodeString(response.Output, PageRequestManager.PageRedirectToken, String.Empty, redirectLocation); } else if (RestHandlerFactory.IsRestRequest(context)) { // We need to special case webservice redirects, as we want them to fail (always are auth failures) RestHandler.WriteExceptionJsonString(context, new InvalidOperationException(AtlasWeb.WebService_RedirectError), (int)HttpStatusCode.Unauthorized); } }