Response.Redirect results in “Object moved to here”

百般思念 提交于 2019-11-29 02:50:11

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.

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

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.

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.

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

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.

Kursat Turkay

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.

DIZAD

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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!