ASP.NET 中失去向后导航

发布于 2024-08-03 04:46:46 字数 568 浏览 6 评论 0原文

我有一个带有“提交”按钮的 ASP.NET2.0 网页。 当用户单击时,我会即时生成一个 XML 文件并将其作为结果返回。

这是代码:

protected void submitBtn_Click(object sender, EventArgs e)
    {
        string result = this.ProduceMyXmlResult();

        this.Response.Clear();
        this.Response.StatusCode = 200;
        this.Response.ContentType = "application/xml";
        this.Response.ContentEncoding = System.Text.Encoding.UTF8;
        this.Response.Write(result);
        this.Response.End();
    }

这段代码正是我想要的。但是,浏览器不会将该 XML 文件识别为新页面,因此“后退”按钮不会将我带回到原始页面。为什么以及如何克服这个问题?

I have an ASP.NET2.0 web page with a Submit button.
When the user clicks, I generate an XML file on the fly and return that as a result.

Here is the code:

protected void submitBtn_Click(object sender, EventArgs e)
    {
        string result = this.ProduceMyXmlResult();

        this.Response.Clear();
        this.Response.StatusCode = 200;
        this.Response.ContentType = "application/xml";
        this.Response.ContentEncoding = System.Text.Encoding.UTF8;
        this.Response.Write(result);
        this.Response.End();
    }

The piece of code does exactly what I want. However, the browser does not recognize the XML file as a new page, so the BACK button does not take me back to my original page. Why and how can I overcome that?

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

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

发布评论

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

评论(1

始终不够 2024-08-10 04:46:46

我认为,最简单的方法是创建一个单独的页面,在 Page_Load() 上执行此代码,并在按下按钮时重定向到该页面。

没有向后导航的原因是浏览器不知道页面已更改。由于“提交”按钮正在执行回发,并且您将返回 XML 数据作为对该回发的响应,因此在浏览器看来,这似乎只是当前页面的某种转换(就像您更改了Label 控件的文本)。

完成此操作的“正确”方法是使用某种类型的 HTTP 处理程序,但我没有经验来建议正确的方法,并且您已经拥有此方法的工作 C# 代码隐藏。

The simplest way to do so, I think, would be to create a separate page that executes this code on Page_Load(), and redirect to it when the button is pressed.

The reason you have no backward navigation is because the browser is unaware the page has changed. Since the Submit button is preforming a postback, and you are returning XML data as the response to that postback, it appears to the browser as though this is just some transformation of the current page (just as though you'd, say, changed the text of a Label control).

The "correct" way to accomplish this would be with some type of HTTP handler, but I haven't the experience to suggest the proper way to do so and you already have working C# code-behind for this method.

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