当指定另一个页面时重定向到 webapp 默认文档?

发布于 2024-08-20 16:14:44 字数 1258 浏览 7 评论 0原文

IIS6、ASP.NET 2.0、无表单身份验证

我正在调用 Response.Redirect("~/foo.aspx"),但出现了我的站点的默认文档 ("Default.aspx")。更糟糕的是,这种情况只是间歇性地发生。有时重定向会显示正确的页面。

我检查了会话状态,但在 web.config 中没有看到任何值(也就是说,我假设我使用的是 20 分钟默认值)。

我希望有更多相关信息可以分享(我会尽力回答任何问题)。

有什么想法吗?为什么没有重定向到指定页面?

编辑:我更深入地研究了代码并了解了更多细节。

好的。有 foo.aspx 和 foo2.aspx(以及默认文档 Default.aspx)。所有页面都从 BasePage 扩展,而 BasePage 又扩展了 Page。

BasePage 有一个名为 ReturnPage 的属性:

protected string ReturnPage {
    get {
        if (Session["ReturnPage"] == null) {
            Session["ReturnPage"] = "";
        }
        return Session["ReturnPage"].ToString();
    }
    set { Session["ReturnPage"] = value; }
}

用户单击 foo.aspx 上的 LinkBut​​ton,单击事件处理程序以两行代码结束:

ReturnPage = ResolveUrl("~/foo.aspx");
Response.Redirect(ResolveUrl("~/foo2.aspx"));

foo2.aspx 的 Page_Load 有问题,其错误处理调用 Response.Redirect(ReturnPage)。

当我查看foo2.aspx的响应头时,302位置是string.Empty(也就是说,没有)。该响应标头与 foo.aspx 的响应具有相同的 ASP.NET 会话 ID。

请记住——这是间歇性的。有时,您可以单击该 LinkBut​​ton 并轻松转到 foo2.aspx,没问题。您可以使用完全相同的数据处理一次点击,但它会失败。您将从默认文档(Default.aspx,“bug”发送给您的位置)导航回 foo.aspx,再次单击相同的数据(网格/表中的同一行 - 相同的 LinkBut​​ton,本质上),并且您将毫无问题地重定向到 foo2.aspx。

IIS6, ASP.NET 2.0, No Forms Authentication

I'm calling Response.Redirect("~/foo.aspx"), but the default document ("Default.aspx") for my site is appearing. To make matters worse, it only happens intermittently. Sometimes the redirect displays the right page.

I've checked session state, and I don't see any values in the web.config (that is, I'm assuming I'm using the 20-minute defaults).

I wish I had more relevant information to share (I'll do my best to answer any questions).

Any ideas? Why isn't it redirecting to the specified page?

EDIT: I've looked deeeeeper into the code and learned more details.

Ok. There's foo.aspx and foo2.aspx (and the default document, Default.aspx). All pages extend from BasePage, which extends Page.

BasePage has a property named ReturnPage:

protected string ReturnPage {
    get {
        if (Session["ReturnPage"] == null) {
            Session["ReturnPage"] = "";
        }
        return Session["ReturnPage"].ToString();
    }
    set { Session["ReturnPage"] = value; }
}

Users click on a LinkButton on foo.aspx, and the click event handler ends with two lines of code:

ReturnPage = ResolveUrl("~/foo.aspx");
Response.Redirect(ResolveUrl("~/foo2.aspx"));

The Page_Load of foo2.aspx has problems, and its error handling calls Response.Redirect(ReturnPage).

When I view the response headers of foo2.aspx, the 302 location is string.Empty (that is, there isn't one). That same response header has the same ASP.NET Session ID as the response of foo.aspx.

And remember -- this is intermittent. Sometimes, you can click on that LinkButton and go effortlessly to foo2.aspx, no problem. You can process the click with the exact same data once, and it will fail. You'll navigate from the default document (Default.aspx, where you were sent by the "bug") back to foo.aspx, click again with the same data (the same row in the grid/table -- the same LinkButton, essentially), and you'll be redirected to foo2.aspx without issue.

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

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

发布评论

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

评论(4

眼眸印温柔 2024-08-27 16:14:45

当你说:

有时重定向会显示正确的页面。

它是否只是发生了,并且您不确定是否有某些页面受到该问题的影响?如果是这种情况,那么您可能遇到了寻址问题。您可以使用相对路径或绝对路径,而不是应用程序相对路径。我还猜测您正在尝试从网站上的子目录定向到页面或网站上的子目录。如果您选择坚持使用应用程序相对路径,请确保考虑到子目录。 (例如:~/FooPages/Foo.aspx)

这是我刚刚找到的一个很好的参考页面:
http://nathanaeljones.com/129/types-of-asp-net -路径/

When you say:

Sometimes the redirect displays the right page.

Does it just happen, and you are not sure if there are certain pages that are affected by the problem? If this is the case, then you probably have a addressing problem. You can use either a relative path or an absolute path rather than an Application-relative path. I would also guess that you are trying to either direct to a page from a subdirectory on your site or to a subdirectory on your site. If you choose to stick with the Application-relative path make sure that are taking the subdirectory into account. (ex: ~/FooPages/Foo.aspx)

Here is a good reference page I just found:
http://nathanaeljones.com/129/types-of-asp-net-paths/

时光暖心i 2024-08-27 16:14:45

我在这里有点困惑。您到底想实现什么目标?您得到的正是默认文档,因为 302 为空。您的“不一致”行为几乎肯定是由于您在会话中保存数据的方式造成的。

这里真正的问题是为什么当 foo2.aspx“有问题”时要重定向。这里有什么问题?为什么要重定向?如果确实需要重定向,为什么要更改重定向目标?将其设为静态错误报告页面就可以了。

I'm a little confused here. What exactly are you trying to accomplish? You're getting the default document exactly because the 302 is blank. Your "inconsistent" behavior is almost certainly due to the way you are saving data in the Session.

The real issue here is why you're redirecting when foo2.aspx "has problems". What's the problem here? Why redirect? If you really need to redirect, why is the redirect target changed? Make it a static error reporting page and you'll be fine.

长伴 2024-08-27 16:14:45

一旦您重定向并从 foo2.aspx 获取 BasePage 的新实例,ReturnPage 属性不会再次为 null 吗?然后,一旦您的页面加载错误并尝试重定向,它将访问空字符串。也许尝试在会话中抛出该属性

Session.Add("ReturnPage","~/foo.aspx") 

而不是

ReturnPage = ResolveUrl("~/foo.aspx");

当然,您必须修改页面加载中的错误处理以将其从会话而不是属性中获取,并且您可能必须重新考虑整个重定向在系统中的工作方式,如果这事实证明这是问题所在。

编辑:
要测试有关属性未设置或正确设置的想法......(只是为了测试,我并不是建议您应该对其中的路径进行硬编码),将您的 getter 更改为下面的示例,然后检查是否有用。希望这会有所帮助,我很想知道问题是什么(如果这不是问题)。

get { 
   if (Session["ReturnPage"] == null) { 
        Session["ReturnPage"] = "~/foo.aspx"; 
    } 
    return Session["ReturnPage"].ToString(); 

} 

Once you redirect and get a new instance of the BasePage from foo2.aspx, won't that ReturnPage property be null again? Then once your page load errors out and tries to redirect it will be accessing a null string. Maybe try throwing that property in the Session

Session.Add("ReturnPage","~/foo.aspx") 

instead of

ReturnPage = ResolveUrl("~/foo.aspx");

Ofcourse you would have to modify that error handling in the page load to grab it out of session rather than the property and you may have to rethink how the whole redirect is working in your system if this turns out to be the issue.

EDIT:
To test this idea about the property not getting set, or getting set correctly....(just to test I am not suggesting you should hard code the path in there), change your getter to the example below, then check to see if it works. Hope this helps, I am curious to find out what the problem is if this is not the issue.

get { 
   if (Session["ReturnPage"] == null) { 
        Session["ReturnPage"] = "~/foo.aspx"; 
    } 
    return Session["ReturnPage"].ToString(); 

} 
帅气尐潴 2024-08-27 16:14:44

在 Response.Redirect() 之前立即在会话中放置一个值是有风险的。

将 foo.aspx 的 Response.Redirect() 更改为以下内容可能会更可靠地保留会话值:

Response.Redirect("~/foo2.aspx", false);

更新:这个问题最终只能通过将我们的会话状态转移到 SQL Server 中来解决。请参阅相关问题:为什么/何时会话写入容易受到线程终止的影响?

Placing a value in the session immediately before a Response.Redirect() is risky.

Changing foo.aspx's Response.Redirect() to the following might retain the session value more reliably:

Response.Redirect("~/foo2.aspx", false);

UPDATE: This ended up being fixed only by moving our session state into SQL Server. See related question: Why/when are session writes vulnerable to thread termination?

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