Ole Albers
Ole Albers

Reputation: 9305

Prevent aspx-page rendering

I got a "normal" ascx-Page which contains HTML-Parts as well as code behind. These elements should all be shown in normal conditions (works).

Now I want to be able to set a request-parameter which causes the page zu render differently. Then it sends the information on that page not human-readable but for a machine:

 string jsonProperty = Request["JSonProperty"];
                if (!string.IsNullOrEmpty(jsonProperty))
                {                    
                    Response.Clear();
                    Response.Write(RenderJSon());
                  //  Response.Close();
                    return;

This code is inside the Page_PreRender. Now my problem is:The string is correctly sent to the browser but the "standard" html-content is still rendered after that.

When I remove the "Response.Close();" Comment I receive an "ERR_INVALID_RESPONSE"

Any clue how to solve this without creating an additional Page?

Upvotes: 1

Views: 4337

Answers (4)

binki
binki

Reputation: 8316

It is recommended to avoid calling Response.End() even if it results in the correct behavior. This is because that method is provided for backwards compatibility with old ASP and was not designed to support performance. To prevent further processing, it throws a ThreadAbortException, trashing the thread, forcing the system to start a new thread later when processing a new request instead of being able to return that thread to the ASP.NET thread pool. The recommended alternative to stop further processing of the ASP.NET pipeline is to call HttpApplication.CompleteRequest() and return immediately. However, this does not terminate processing of the ASP.NET page.

To prevent further content from being sent to the client, one can certainly set Response.SuppressContent to true. That will prevent the contents of the .aspx file from being sent to the client. However, the .aspx will still be rendered. This includes running logic in the .aspx which may rely on data you’d load in your Load event.

To avoid rendering, you can set Page.Visible to false. This causes the call to Rendercontrol() to be skipped.

For this to work, you need to move your logic into the Load() event instead of PreRender() event.

With your code, that would be:

protected void Page_Load(object sender, EventArgs e)
{
    var jsonProperty = Request["JSonProperty"];
    if (!string.IsNullOrEmpty(jsonProperty))
    {
        Response.ContentType = "application/json";
        Response.Write(RenderJSon());
        Context.ApplicationInstance.CompleteRequest();
        Visible = false;
        return;
    }
}

Upvotes: 2

timothy
timothy

Reputation: 588

Can i suggest that Response.End() can throw an error.

Use Response.SuppressContent = true; to stop further processing of "standard" html

string jsonProperty = Request["JSonProperty"];
if (!string.IsNullOrEmpty(jsonProperty))
{                    
    Response.Clear();
    Response.ContentType = "application/json";
    Response.Write(RenderJSon());

    Response.Flush();                    // Flush the data to browser
    Response.SuppressContent = true;     // Suppress further output - "standard" html-
                                         // content is not rendered after this

    return;
} 

Upvotes: 6

Richard
Richard

Reputation: 8290

Have you tried setting the ContentType to application/json and End'ing the response, like so:

string jsonProperty = Request["JSonProperty"];
if (!string.IsNullOrEmpty(jsonProperty))
{                    
    Response.Clear();
    Response.ContentType = "application/json";
    Response.Write(RenderJSon());
    Response.End();

    return;
}    

Upvotes: 1

Claudio Redi
Claudio Redi

Reputation: 68440

Try adding Response.End()

Sends all currently buffered output to the client, stops execution of the page, and raises the EndRequest event.

Also, as @Richard said, add

context.Response.ContentType = "application/json";

Upvotes: 1

Related Questions