36

I'm working on a C# ASP.NET page that normally ends up redirecting to a "file:" URL. This seems to work fine the majority of the time, in the majority of circumstances, but occasionally (and, on my test system, apparently always) instead of a redirect to a file I get a page with the text "Object moved to here", where "here" is a link to the file that I was trying to redirect to, but with four slashes after the colon instead of two (i.e. "file:////testserver/docs/testdoc.doc")

This is normally accompanied by a "System.Threading.ThreadAbortException: Thread was being aborted" message.

I've looked for a solution elsewhere and found out some interesting stuff about Response.Redirect causing ThreadAbort exceptions, but that doesn't seem to be the fundamental problem - it seems to me that the actual problem is the "Object moved to here" message, which causes the exception to be thrown.

Anybody got any suggestions why I'm getting that...?

EDIT: Forgot to mention I'm running Firefox (3.5.7) with IE Tab, so was about to mention that when I thought I'd better try it in IE, and voila - it works in IE (7).

3

8 Answers 8

31

Just for future reference another reason this can occur is if you do something like Response.Redirect(null) or similar. I had a situation where my variable holding the URL was null and this is what I got.

2
  • in my case i posted null parameter to an REST api and the response was redirect to null URL instead of generated url
    – Shaiju T
    Dec 8, 2015 at 17:10
  • This also applies when having an empty string. Just experienced that today.
    – Marcel
    May 30, 2018 at 6:17
17

This may caused by putting the Response.Redirect() method in try-catch block. The solution I came along was to End the response virtually by flushing a redirect header to the client. take a look:

HttpResponse Response = HttpContext.Current.Response;
Response.StatusCode = 301; 
Response.StatusDescription = "Moved Permanently";
Response.RedirectLocation = "YourRedirectionUrlHere.aspx";
Response.Flush();
2
  • This works great for me (Used it to generate and return a webcal:// protocol iCal file) though I didn't need to set the StatusDescription property Jan 3, 2013 at 16:25
  • 1
    This is also an answer for this question: stackoverflow.com/questions/347281/…
    – Hakan KOSE
    Jul 26, 2013 at 12:08
7

I've just come across a case where this is happening. Turns out we had some code that effectively did:

if (condition)
{
  Response.Redirect(page1);
}
Response.Redirect(page2);

Obviously the person who wrote this (a good while ago fortunately) didn't realise that a Response.Redirect does not, by default, end the thread.

I've no idea what the consequences of doing this are but a fiddler trace of this happening looks to show a corrupt redirect. This might be a co-incidence of course but this is the only place we've seen this issue.

1
  • Response.Redirect DOES end the thread by defaul.
    – ClassY
    Sep 6, 2022 at 9:19
3

Another reason this might happen is that you are redirecting from an https page to a http page. Changing the redirect URL to also be https:// fixed the problem for me.

0
3

This did the trick for me when I saw this issue:

[Route("/something/{param}", "GET")]
public class MyRequestArg{
   public string param{get;set;}
}

public class MyRequestService
{
    public object Get(MyRequestArg request)
    {
    var url = "http://www.zombo.com";
    var myCookieString = "anything is possible!";

    var result = new HttpResult
                 {
                   StatusCode = HttpStatusCode.Redirect,
                   Headers = {
                              {HttpHeaders.Location, url},
                              {HttpHeaders.SetCookie, myCookieString}
                             }   
                 };
    return result;
    }
}
1

In MVC, you might see this after a RedirectToRoute().

If you use a tool like Fiddler, you should see a problem with the server response. I noticed a 500 Error.

In my case, this was caused by an object being added to Session that was NOT Serializable.

0

Use anchor element with runat=server

<a runat="server" ID="anchor1">anything can be here</a>

In code behind :

if (!ispostback)
  anchor1.href="whateveryoulink";

Give it a try.

Its working better than the previous Status Code=301 method.

0

I fixed this issue by setting my global string variable to static, because my URL was resetting to empty when I was redirecting.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.