Don’t forget that Response.End()

While adding some few enhancement on an old project, ASP.NET webform-based API, I was having problem removing the HTML tag output from the API. I have written this project five years ago and I know that you can write a response from an ASPX page without the HTML tags. I simply should use the Response.Clear() or Response.ClearContent(). Response.Clear() is not working?!?!?!

Here is the code that does not output what I want.

protected void Page_Load(object sender, EventArgs e)
{
     //Erases any buffered HTML output
     Response.Clear();
     Response.Write("Hello, API.");
}

If you view the source of the output,

Hello, API.


<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
    <head>
        <title></title>
    </head>
    <body>
<form method="post" action="NewApi.aspx" id="form1">
<div class="aspNetHidden">
                <input type="hidden" name="__VIEWSTATE" id="__VIEWSTATE" value="YpQKYTR2RvrptiLm1lmMsAJwJ//a75ZBpZNGl/rMG5KPseJDjWPtzsRumtqz2U5cLtK9ilxYWrQxgjcxR9VyjfQzP4W4MK8Xgdd753gdoh4=" />
            </div>
<div></div>
<div class="aspNetHidden">
                <input type="hidden" name="__VIEWSTATEGENERATOR" id="__VIEWSTATEGENERATOR" value="F48D0FAC" />
            </div>
        </form>
        <!-- Visual Studio Browser Link -->
        <script type="application/json" id="__browserLink_initializationData">
    {"appName":"Chrome","requestId":"dacea9113ab74bf7a121e7e8599c93a5"}
</script>
        <script type="text/javascript" src="http://localhost:52028/b08ad1d741774013a9875e2b2003daf7/browserLink" async="async"></script>
        <!-- End Browser Link -->
    </body>
</html>

Using my old arsenal of restarting and clearing cache did not work. So I did look up my old code and sure enough I forgot to write the most important line of code. The Response.End()

The working code

protected void Page_Load(object sender, EventArgs e)
{
     //Erases any buffered HTML output
     Response.Clear();
     Response.Write("Hello, API.");
     //Sends all currently buffered output
     //and stops the execution of the page
     Response.End();
}

And I got the correct output.

Hello, API.

So, don’t forget that Response.End();

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s