1

这可能真的很hacky,但我开始绝望了

是否可以防止请求实际到达实际页面...即,我可以在 Application_BeginRequest 中编写一些东西来处理我想要的部分,然后跳过生命周期的其余部分吗?

哦,是否有可能以 ajax 更新面板确实抛出一个合适的方式来做到这一点(返回一些默认内容,“我没有做任何对不起的事情”)

好的,疯狂的问题结束了。

4

3 回答 3

2

当然,只需调用 Response.End():

protected void Application_BeginRequest(object sender, EventArgs e)
{
    if (Request.Url.AbsolutePath.Equals("/PageYouWantToSuppress.aspx", StringComparison.OrdinalIgnoreCase))
    {
        // Do your stuff

        Response.End();
    }
}

ETA:我不确定您问题的 UpdatePanel 部分。

于 2009-10-15T15:52:49.867 回答
2

您可以阻止部分代码在部分页面更新期间执行,如下所示:

if (! ScriptManager.IsInAsyncPostBack) {
    // Code in here won't happen during UpdatePanel actions
}

UpdatePanel 按照设计执行大部分 ASP.NET 页面生命周期。如果您中止该过程(Response.End()例如通过),则请求不会完成,因此客户端将无法获取更新页面 HTML 所需的数据。

当您在 UpdatePanel 中触发服务器端方法时,ASP.NET 会执行以下操作:

  1. 通过运行正常的 ASP.NET 页面生命周期从头开始重建面板中的页面部分(好吧,稍微删节,但很接近)。
  2. 将面板的 HTML 发送回客户端。
  3. 使用 javascript DOM 操作在客户端上重建面板。

(For more see my recent answer to this question, and see UpdatePanel Control Overview or this article for details)

There are several alternatives to the UpdatePanel AJAX implementation that don't involve going through the whole page lifecycle. One of the most popular is Using jQuery to directly call ASP.NET AJAX page methods.

Using jQuery for AJAX calls gets you a little closer to the metal (so to speak) than UpdatePanels, but there's tons of good help to get you started - on StackOverflow itself and on Encosia (the source of that last link).

于 2009-10-15T16:10:53.140 回答
1

考虑在请求处理流的前面编写一个 HTTPModule 来处理这个问题。

于 2009-10-15T15:49:57.547 回答