37

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).

gkrogers
  • 8,126
  • 3
  • 29
  • 36
  • Does this happen in multiple browsers? Have you tried running Fiddler to monitor the request/response, to see the raw HTTP response? – Seth Petry-Johnson Feb 02 '10 at 14:19
  • What version of .NET are you using? This seems like it was a common issue in the 1.1 days... – Seth Petry-Johnson Feb 02 '10 at 14:22
  • Here are some related SO posts: [http://stackoverflow.com/questions/922648/response-redirect-not-always-redirecting](http://stackoverflow.com/questions/922648/response-redirect-not-always-redirecting) [http://stackoverflow.com/questions/1497285/how-do-i-redirect-a-browser-to-a-local-file-in-asp-net](http://stackoverflow.com/questions/1497285/how-do-i-redirect-a-browser-to-a-local-file-in-asp-net) – Seth Petry-Johnson Feb 02 '10 at 14:23

8 Answers8

33

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.

Dan Diplo
  • 25,076
  • 4
  • 67
  • 89
  • 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 08 '15 at 17:10
  • This also applies when having an empty string. Just experienced that today. – Marcel May 30 '18 at 06: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();
Arash Milani
  • 6,149
  • 2
  • 41
  • 47
  • 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 – Luke Merrett Jan 03 '13 at 16:25
  • 1
    This is also an answer for this question: http://stackoverflow.com/questions/347281/asp-net-custom-404-returning-200-ok-instead-of-404-not-found – Hakan KOSE Jul 26 '13 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.

Chris Simpson
  • 7,821
  • 10
  • 48
  • 68
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.

cometfish
  • 523
  • 2
  • 6
  • 15
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;
    }
}
Wes
  • 1,059
  • 13
  • 18
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.

cat5dev
  • 1,317
  • 15
  • 12
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.

Zen Of Kursat
  • 2,672
  • 1
  • 31
  • 47
0

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

TylerH
  • 20,799
  • 66
  • 75
  • 101
dizad87
  • 448
  • 4
  • 15