ContentType
This property sets an HTTP header—it is useful when sending a response that is not HTML. By using ContentType
we change the HTTP header for the response.
So when you set the ContentType
property, how does this affect the HTTP headers sent by IIS? We will see this header being sent: "Content-Type: text/plain".
Here the Application_BeginRequest
event handler is defined. In Application_BeginRequest
, we acquire the HttpResponse
, and then assign the ContentType
property to "text/plain".
Response.Write
method and call CompleteRequest
.using System; using System.Web; namespace WebApplication1 { public class Global : HttpApplication { void Application_BeginRequest(object sender, EventArgs e) { // Get response. HttpResponse response = base.Response; // Set ContentType. response.ContentType = "text/plain"; // Write string. response.Write("PLAIN TEXT"); // Complete request. base.CompleteRequest(); } } }
When this is executed successfully, you will see a plain text page in your web browser with the PLAIN TEXT in monospaced font.
We sent a different Content-Type HTTP header in the ASP.NET environment. By assigning to the ContentType
property, the response will be sent with your specified value.