During a recent Web project, I focused on the coding,
while a Flash developer handled the user interface. The interface required data
delivered from a backend data source and the Flash developer requested text
files. This was an unsuitable delivery method since the data varied according
to the user. I convinced him to call an ASP.NET page, where I manipulated the
page’s content type to text rather than standard HTML. I was surprised when
discussing the project with other developers who seemed unaware of this
approach. I’ll describe the method in detail in this article.
The type of content
When a Web client requests a page, the server informs the
client of the type of data it is sending. It is returned via the HTTP
content-type header. Most servers are configured to send the content type
according to file extension or MIME type.
In Internet Information Services
(IIS), you can add new file types via the properties dialog box of the
default Web site. The HTTP Headers tab contains a File Types button. You can use
it to add new file types. Here is a sampling of some common file types:
- css: A cascading style sheet
- doc: A Microsoft Word document.
- html: A standard HTML file.
- jpg: An image file.
- mpg: A video file.
The HTTP protocol does not use the file extension to
determine the type of content contained in a file; after all, some files may
not have file extensions. Instead, the Web server specifies the correct MIME
type using a content-type header when responding to requests.
MIME types identify the type of data contained in a file.
MIME types are similar to file extensions, but they are more universally
accepted. In our previous list, the MIME type for css
content is text/css; text/html is the type for an
html file; and image/jpeg is the type for jpg files.
While Web servers often use the file extension as previously discussed, a MIME
type should always take precedence. The Internet Assigned Numbers Authority
(IANA) provides a thorough MIME reference list online.
Now let’s examine how to manipulate the content type for an
Controlling ASP.NET content
The HttpResponse class controls
the various aspects of ASP.NET’s response to HTTP
requests. It allows you to easily access the output sent to the client as well
as other attributes. This includes the character set, encoding, and the content
The ContentType property of the HttpResponse class provides access to the MIME type of the
response. It allows you to retrieve or set its value, and the value must be a
valid MIME type. The C# code sample in Listing A sends
data read from the sample SQL Server Northwind
database as a text file. Listing B
contains the equivalent VB.NET code.
This simple example demonstrates the use of the ContentType property. The code connects to SQL Server and
reads all records from the Customers table in the Northwind
database. All data is sent to the browser with the ContentType
property of the Response object set to the text/plain MIME type to signal text.
The code also allows text to be generated by ASP.NET while utilizing ASP.NET
programming. That is, you could take advantage of ASP.NET security or other
features to control access to the page, making it much more powerful than a
generic text file.
While the Response class provides easy access to the ContentType property, you may also specify it via the page
directive located at the top of the ASP.NET page. ContentType
is a property of the page directive. The following line could be used to signal
the type of content sent to the client:
<%@ Page language="c#" ContentType="text/plain" %>
Using this approach, the example code could take the format
that’s in Listing C.
demonstrates a shortcut for exporting data to Excel (assuming Excel is installed on the client machine). The code duplicates the
previous example, but the content type is set to application/vnd.ms-excel to specify a Microsoft Excel file. Note that
tabs are used as separators (/t) as opposed to a comma, and the user is
prompted to open the content in Excel.
Browser behavior does not always follow the rules. While you
may specify text, an image file, or another MIME type as the content for a
page, the browser may choose to treat a file differently. A good example is my
recent experience with IE 6. I tested the output of text in the browser, and IE
would often mistake the text output (specified as text/plain MIME type) as XML
and notify me of errors in the XML. I used Firefox
for testing as well, and it handled the files as planned. This wasn’t a big
problem for my project since the browser was only used for testing, but it
could hamper a different project. Check out this
good discussion of these anomalies.
Deliver the right data
Different projects call for different data and formats. In
order to ensure that you’re delivering the right type of data, you should use
the HTTP content type header. You can access this header via the HttpResponse class or using the ASP.NET page directive. In
addition, you can manipulate the content as necessary using MIME types.
Miss a column?
Check out the .NET Archive, and catch up on the most recent editions of Tony Patton’s column.