如何覆盖 ASP.NET 中的页面加载生命周期以防止加载 ASPX 表单?

发布于 2024-07-17 08:02:41 字数 212 浏览 7 评论 0原文

我需要在多个 ASPX 代码隐藏文件中测试某个条件,并且在某些情况下,希望完全绕过正常的页面加载过程,以便不加载相应的 ASPX 页面。 相反,我想向浏览器发送通过代码隐藏方法编写的自定义响应。

有谁知道从哪里开始 - 页面生命周期中要覆盖哪些方法以及确保在正常 ASPX 页面内容被抑制时将我的自定义 Response.Write 发送到浏览器的最佳技术?

谢谢。

I need to test a condition in several ASPX code-behind files and, in some cases, would like to completely bypass the normal page load process so that the corresponding ASPX page is not loaded. Intead, I'd like to send a custom response to the browser that's written from a code-behind method.

Does anyone know where to start- what method(s) in the page lifecycle to override and the best technique to ensure that my custom Response.Write is sent to the browser while the normal ASPX page content is suppressed?

Thanks.

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

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

发布评论

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

评论(3

一绘本一梦想 2024-07-24 08:02:41

可能是最简单的方法 - 使用 Page_Load()

protected void Page_Load(object sender, EventArgs e)
{
    bool customResponse = true;
    if (customResponse)
    {
        Response.Write("I am sending a custom response");
        Response.End(); //this is what keeps it from continuing on...
    }
}

Probably the easiest way to do it - use Page_Load().

protected void Page_Load(object sender, EventArgs e)
{
    bool customResponse = true;
    if (customResponse)
    {
        Response.Write("I am sending a custom response");
        Response.End(); //this is what keeps it from continuing on...
    }
}
瘫痪情歌 2024-07-24 08:02:41

使用 Response.End() 的“简单”方法对性能来说很糟糕,会抛出终止线程的异常。

http://blogs.msdn.com/b/tmarq/archive/2009/06/25/ Correct-use-of-system-web-httpresponse-redirect.aspx

http://weblogs.asp.net/hajan/archive/2010/09/26/why-not-to-use-httpresponse-close-and-httpresponse-end.aspx

我有同样的问题并以这种方式解决了。 这是一个两步过程:首先调用 HttpApplication.CompleteRequest() 并退出处理。 接下来重写 Render() 以便不调用基本方法。 示例代码则变为:

bool customResponse = true;

protected void Page_Load(object sender, EventArgs e)
{
    if (customResponse)
    {
        Response.Write("I am sending a custom response");
        this.Context.ApplicationInstance.CompleteRequest();
        return;   // Bypass normal processing.
    }
    // Normal processing...
}

protected override void Render(HtmlTextWriter writer)
{
    if (!customResponse)
        base.Render(writer);        // Then write the page as usual.
}

The "easy" way to do it, with Response.End() is terrible for performance, throwing an exception which terminates the thread.

http://blogs.msdn.com/b/tmarq/archive/2009/06/25/correct-use-of-system-web-httpresponse-redirect.aspx

http://weblogs.asp.net/hajan/archive/2010/09/26/why-not-to-use-httpresponse-close-and-httpresponse-end.aspx

I had the same question and solved it this way. It's a two-step process: First call HttpApplication.CompleteRequest() and exit your processing. Next override Render() so that the base method is not called. The example code then becomes:

bool customResponse = true;

protected void Page_Load(object sender, EventArgs e)
{
    if (customResponse)
    {
        Response.Write("I am sending a custom response");
        this.Context.ApplicationInstance.CompleteRequest();
        return;   // Bypass normal processing.
    }
    // Normal processing...
}

protected override void Render(HtmlTextWriter writer)
{
    if (!customResponse)
        base.Render(writer);        // Then write the page as usual.
}
葵雨 2024-07-24 08:02:41

这实际上取决于您要回复的内容,是发布的表单字段、身份验证信息等...?
使用 Page_Load 显示的方法将起作用,但页面生命周期中该点之前的任何内容也将执行。

It really depends what you're responding to, is it a posted Form field, authentication info etc...?
The method shown using Page_Load will work, but anything before that point in the page lifecycle will also execute.

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