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

发布于 2024-08-19 15:40:43 字数 513 浏览 2 评论 0原文

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

这通常是伴随着“System.Threading.ThreadAbortException:线程正在中止”消息。

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

有人对我为什么得到这个有任何建议吗...?

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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(8

浅黛梨妆こ 2024-08-26 15:40:44

使用 runat=server 锚元素

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

在代码后面

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

:尝试一下。

它比之前的 Status Code=301 方法效果更好。

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.

A君 2024-08-26 15:40:44

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

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

皓月长歌 2024-08-26 15:40:43

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

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.

请远离我 2024-08-26 15:40:43

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

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

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();
李白 2024-08-26 15:40:43

我刚刚遇到一个案例,就是这样的情况。事实证明,我们有一些有效执行此操作的代码:

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

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

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

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.

-小熊_ 2024-08-26 15:40:43

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

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.

番薯 2024-08-26 15:40:43

当我看到这个问题时,这对我有用:

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

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;
    }
}
蹲在坟头点根烟 2024-08-26 15:40:43

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

如果您使用像 Fiddler 这样的工具,您应该会看到服务器响应出现问题。我注意到500 错误

就我而言,这是由于添加到 Session 中的不可序列化对象引起的。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文