Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When should I set HttpResponse.SuppressContent to true

I wrote HTTP module which validates url and in case if it has special symbols I'm redirecting to the root.

I found different examples how to do it and someone suggested to set HttpResponse.SuppressContent property to true. But I'm not sure what will happen in that case. Msdn says that it's indicating whether to send HTTP content to the client, so does it mean that redirect will not be initiated by client, but by server ?

like image 820
Jorgen Avatar asked May 01 '15 22:05

Jorgen


1 Answers

ASP.Net buffers the output of a Handler by default. Response.SupressContent prevents the sending of that content to the client, but headers will still be sent.

Example 1 - Output with buffering and SupressContent=false

<%@ Page Language="C#" AutoEventWireup="true"  %>
<script runat="server">
protected void Page_Load(object sender, EventArgs e)
{
    Response.Write("No Supression");
}
</script>
Dynamic Content
<%= DateTime.Now %>

Raw Http Response:

HTTP/1.1 200 OK
Cache-Control: private
Content-Type: text/html; charset=utf-8
Vary: Accept-Encoding
Date: Sun, 03 May 2015 23:29:17 GMT
Content-Length: 44

No Supression
Dynamic Content
5/3/2015 5:29:17 PM

Example 2 - Output with buffering and SupressContent=true

<%@ Page Language="C#" AutoEventWireup="true"  %>
<script runat="server">
protected void Page_Load(object sender, EventArgs e)
{
    Response.Write("Suppress it all!");
    Response.SuppressContent = true;
}
</script>
Dynamic Content
<%= DateTime.Now %>

Raw Http Response:

HTTP/1.1 200 OK
Cache-Control: private
Content-Type: text/html
Date: Sun, 03 May 2015 23:34:13 GMT
Content-Length: 0

Notice how this output has no content.

Your question was specifically about what happens with a redirect.

Example 3 - Redirect when SupressContent=true

<%@ Page Language="C#" AutoEventWireup="true"  %>
<script runat="server">
protected void Page_Load(object sender, EventArgs e)
{
    Response.Redirect("~/SomewhereElse.aspx");
    // Exception was thrown by previous statement. These two lines do not run.
    Response.Write("Redirect Supress Content");
    Response.SuppressContent = true;
}
</script>
Dynamic Content
<%= DateTime.Now %>

Raw Http Response:

HTTP/1.1 302 Found
Cache-Control: private
Content-Type: text/html; charset=utf-8
Location: /SomewhereElse.aspx
Date: Sun, 03 May 2015 23:35:40 GMT
Content-Length: 136

<html><head><title>Object moved</title></head><body>
<h2>Object moved to <a href="/SomewhereElse.aspx">here</a>.</h2>
</body></html>

Notice how there is still a body. That's because we didn't allow the request to completely process. By default Response.Redirect will throw an exception, and the Response.SupressContent property is not set to true. If we pass false to the second parameter, it looks like the sample above.

Example 4 - Redirect when SupressContent=true, and Response.Redirect does not throw Exception.

<%@ Page Language="C#" AutoEventWireup="true"  %>
<script runat="server">
protected void Page_Load(object sender, EventArgs e)
{
    Response.Redirect("~/SomewhereElse.aspx", false);
    Response.Write("Redirect Supress Content");
    Response.SuppressContent = true;
}
</script>
Dynamic Content
<%= DateTime.Now %>

Raw Http Response:

HTTP/1.1 302 Found
Cache-Control: private
Content-Type: text/html
Location: /SomewhereElse.aspx
Date: Sun, 03 May 2015 23:37:45 GMT
Content-Length: 0
like image 173
JJS Avatar answered Nov 14 '22 23:11

JJS