简体   繁体   中英

ASP.NET Find out which Response.Redirect is redirecting?

I have a huge website with a huge backend. Somewhere, somehow, there is a Response.Redirect called while trying to open a site (debug environment).

Is there a way to find out, which Response.Redirect causes the redirect?

An certain way would be to set a debug breakpoint on every Response.Redirect in the whole website. But this is a lot of effort.

Another idea I had was to stop at the "ThreadAbortException" (which is thrown by Response.Redirect) with "Debug->Exceptions..". But this doesn't work. Seems like the frame or whatever is no longer executed to get a break on it.

Last attempt was to watch the call-stack. But the stack never gets to the last Response.Redirect because it is a new frame call.

Well, I got an idea which solved my problem but required massive code replacement (which is not a problem with 'Find and replace').

I created an static class:

public static class OwnResponse
{
    public static void Redirect(string Url, bool EndResponse = true)
    {
        HttpContext.Current.Response.Redirect(Url, EndResponse); // set the breakpoint here
    }
}

Then I replaced every Response.Redirect in the code with OwnResponse.Redirect. Now I was able to set my breakpoint onto the first line in the class. I called the website and the breakpoint got hit. Then I just watched the call-stack and knew where the redirect occurs.

There is another possible solution, which requires a bit more work. You have to get "Stepping into .NET code" to run. Then you can easily set a break point in the .NET method's first line.

You can use below code in landing page:-

string yourPreviousUrl = Request.UrlReferrer.ToString();

if(!string.IsNullOrEmpty(yourPreviousUrl))
{
    //Referrer was found!
}
else
{
    //Unable to detect a Referrer
}

As mention in official site :-

HttpRequest.UrlReferrer Property

Gets information about the URL of the client's previous request that linked to the current URL.

You could try to implement tracing and save the results to a file. The trace data might help you to pinpoint your redirect.

Here is a link with some more background on ASP.NET tracing:

http://www.codeproject.com/Articles/82290/Step-by-Step-Guide-to-Trace-the-ASP-NET-Applicatio

Use fiddler or another http traffic capture tool and capture the network traffic. You should be able to see the request that was initiated and take it from there.

Open the Exception Settings window and search for "threadabort". Check the checkbox for the ThreadAbortException. Now, when a redirect is executed from code, your debug session will enter break mode.

异常设置

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM