Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Intercept redirects from UpdatePanel Posts

According to below link when you do a 'Response.Redirect' during any posts inside update panel, it would send a HTTPResponseCode of 200 and Ajax javascripty libraries will take over from there to redirect the user to a page.

https://connect.microsoft.com/VisualStudio/feedback/details/542238/redirecting-during-ajax-request-returns-wrong-status-code

However, I'm writing a module in which I'm intercepting all redirects and changing the URLs to contain a value. I'm doing this during PreSendRequestContent event. Below is my code

HttpApplication app = (HttpApplication)sender;

if (app.Response.StatusCode == 302 && !app.Response.RedirectLocation.Contains("MyValue"))
{
   // Add MyValue to URL
}

Is there any way for me to do the same during aforementioned redirects? I'm okay to do this both on client side using onEndRequest or similar events and some other module event on server side

like image 218
Kartheek N Avatar asked Jan 07 '14 05:01

Kartheek N


1 Answers

I did a little testing with an updatepanel, and as far as I can tell, you can determine whether or not the response is being redirected by an updatepanel by looking at the property Response.IsRequestBeingRedirected. This property seems to answer correctly both when seeing redirects from inside updatepanels, and when doing regular redirects. The problem is that when the response is being redirected by an updatepanel, the Response.RedirectLocation is null, which means that you would have to modify the actual contents of the response in order to change the url.

Changing the contents of the response is pretty tricky, as the response output stream is write-only, which means that you can't even grab the contents change them and then overwrite the response. The only way I have found would require you to hook up a filter to the response as described Here. This is, however, somewhat involved, requires quite a lot of code, and the filter must be hooked up before PreSendRequestContent (I don't know exactly when, but before...), which means that you would have to write a filter that takes a reference to the response in order to determine whether or not to transform the content.

Because the filter described above seems like overkill, maybe you could just clean up your javascript a bit by using a regex:

var redirectRegex = /pageRedirect\|{2,}(\S+)\|/;

function changeURL(sender, args) {
    var s = new String(sender._xmlHttpRequest.responseText);

    var match = redirectRegex.exec(s);
    if (match)
    {            
        encodedURL = decodeURIComponent(match[1]);
        window.location.href = DoCustomizationsOntheURL(encodedURL);
        args.stopPropagation();
    }
}

But while the above regex works on my test urls, I can't guarantee that it will work for everything.

Did you consider a static/extension method to transform the url, and just calling that at the point of redirect?

//Extension method.
public static void TransformRedirect(this HttpResponse response, 
                                     string redirectUrl)
{
    //Transform url here.
    response.Redirect(redirectUrl);
}

//And when redirecting.
Response.TransformRedirect("RedirectTarget.aspx");
like image 176
rusmus Avatar answered Oct 19 '22 22:10

rusmus