gpt4 book ai didi

c# - 如何阻止回发在客户端刷新页面

转载 作者:数据小太阳 更新时间:2023-10-29 04:41:06 24 4
gpt4 key购买 nike

当我使用 __doPostBack 开始回发时,会创建一个文件并返回给用户以在 HttpContext.Current.Response 中下载。

因为我更改了 Response,所以包含其 javascript 值的页面没有被修改

但是当我没有文件输出时,页面刷新(因为回发),页面上的javascript修改丢失。

我如何“停止”回发继续并保留我的当前页面?我不能使用异步回发,因为我需要回发让用户下载文件。

编辑:评论中的一些问题后的更多信息:

  • 该文件是在网络服务请求中请求的。网络服务需要执行繁重的查询以确定是否将创建文件。我宁愿这只发生一次。
  • 用户可以拖放一些将在文件中使用的过滤器要求。如果没有文件可用,用户应该能够更改他的过滤器,所以这就是不应更改页面的原因。

最佳答案

来自 W3 标准和 RFC 2616:

10.2.5 204 No Content The server has fulfilled the request but does not need to return an entity-body, and might want to return updated metainformation. The response MAY include new or updated metainformation in the form of entity-headers, which if present SHOULD be associated with the requested variant.
If the client is a user agent, it SHOULD NOT change its document view from that which caused the request to be sent. This response is primarily intended to allow input for actions to take place without causing a change to the user agent's active document view, although any new or updated metainformation SHOULD be applied to the document currently in the user agent's active view. The 204 response MUST NOT include a message-body, and thus is always terminated by the first empty line after the header fields.

注意这里的粗线。我自己没有尝试过;但是,将 HTTP 状态设置为 204 并发回一个空文档,而不是尝试完全停止回发,当然值得一试。

祝你好运,我希望这对你有所帮助。

编辑:这是解决问题的代码:
System.Web.HttpContext.Current.Response.StatusCode = 204;

关于c# - 如何阻止回发在客户端刷新页面,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8745383/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com