views:

826

answers:

2

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.

+5  A: 

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...
    }
}
Terrapin
Response.Clear would also probably be helpful
Ken Browning
A: 

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.