Response.Redirect results in “Object moved to here

2019-02-07 17:48发布

问题:

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

回答1:

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:

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();


回答3:

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.



回答4:

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.



回答5:

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;
    }
}


回答6:

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.



回答7:

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.



回答8:

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