C# Response.Redirect 导致“对象移至此处”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2184596/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-07 00:10:51  来源:igfitidea点击:

Response.Redirect results in "Object moved to here"

c#asp.netexceptionabort

提问by gkrogers

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

我正在处理一个 C# ASP.NET 页面,该页面通常最终重定向到“文件:”URL。在大多数情况下,这似乎在大多数情况下都可以正常工作,但偶尔(并且在我的测试系统上,显然总是如此)而不是重定向到文件,我会得到一个带有文本“对象移至此处”的页面,其中“here”是我试图重定向到的文件的链接,但冒号后有四个斜杠而不是两个(即“file:////testserver/docs/testdoc.doc”)

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

这通常伴随着“System.Threading.ThreadAbortException: Thread was being aborted”消息。

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.

我在别处寻找解决方案,并发现了一些关于 Response.Redirect 导致 ThreadAbort 异常的有趣内容,但这似乎不是根本问题 - 在我看来,实际问题是“对象移动到这里”消息,这会导致抛出异常。

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

编辑:忘了提到我正在使用 IE Tab 运行 Firefox (3.5.7),所以要提到当我认为我最好在 IE 中尝试它时,瞧 - 它在 IE (7) 中工作。

回答by Arash Milani

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

这可能是由于将Response.Redirect()方法放入try-catch块中造成的。我提出的解决方案通过将重定向标头刷新到客户端来虚拟结束响应。看一看:

HttpResponse Response = HttpContext.Current.Response;
Response.StatusCode = 301; 
Response.StatusDescription = "Moved Permanently";
Response.RedirectLocation = "YourRedirectionUrlHere.aspx";
Response.Flush();

回答by Dan Diplo

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.

仅供将来参考,这可能发生的另一个原因是,如果您执行诸如 Response.Redirect(null) 或类似的操作。我有一种情况,我的保存 URL 的变量为空,这就是我得到的。

回答by Chris Simpson

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.

显然,写这篇文章的人(幸运的是不久前)没有意识到 Response.Redirect 默认情况下不会结束线程。

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.

我不知道这样做的后果是什么,但这种情况的提琴手痕迹看起来显示了损坏的重定向。当然,这可能是一个共同事件,但这是我们看到此问题的唯一地方。

回答by Kursat Turkay

Use anchor element with runat=server

使用锚元素 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=301method.

它比以前的Status Code=301方法工作得更好。

回答by cat5dev

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

在 MVC 中,您可能会在RedirectToRoute()之后看到这一点。

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

如果您使用像Fiddler这样的工具,您应该会看到服务器响应有问题。我注意到一个500 Error

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

在我而言,这是由添加到对象而引起会议,这是不可序列

回答by cometfish

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.

这可能发生的另一个原因是您正在从 https 页面重定向到 http 页面。将重定向 URL 也更改为 https:// 为我解决了这个问题。

回答by Wes

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

回答by dizad87

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

我通过将全局字符串变量设置为静态来解决此问题,因为在重定向时我的 URL 重置为空。