Programming Microsoft ASP.NET 4 - Dino Esposito [343]
Write
Writes content to the underlying output stream. The method can write a string, a single character, or an array of characters, as well as an object. In this case, though, what gets written is the output of the object’s ToString method.
WriteFile
Writes the specified file (or a portion of it) directly to the output stream. The file can be identified with its path or a Win32 handle (an IntPtr object). It is subject to failures with very large files. (See the references to this method later in the chapter.)
WriteSubstitution
Allows fragments of a page to be substituted and sent to the output cache. (We’ll cover this method in more detail in Chapter 18.)
Output Caching Features
The HttpResponse class has several methods to make the page response it represents dependent on files or cache item changes. The methods AddFileDependency and AddCacheItemDependency (and their versions that handle multiple dependencies) make the page response invalid when the specified file or files or cached item or items are modified.
This is a simple form of programmatic page output caching, not as powerful as the API that we’ll examine in Chapter 18, but still worth a look. The API discussed in Chapter 18 is superior because it allows you to control how the page response is cached, assigning also the cached output a duration and perhaps a location.
The method AddCacheDependency completes the offering, as it gives you the possibility to make the page response dependent on any dependency object available to your application, including custom dependency objects. See Chapter 18 for more details on custom dependency objects.
Large File Transmission
As you can see, there are three methods for writing potentially large chunks of data down to the output stream: BinaryWrite, WriteFile, and TransmitFile. Of the three methods, TransmitFile is the most stable and reliable, although you won’t notice any significant difference for most files.
Both the WriteFile and BinaryWrite methods seem perfect for streaming binary data down to the client. However, both can put the Web server memory under pressure if called to work on very large files. Why? It’s because both methods load the entire data block (the contents of the file or the byte array) into the Web server’s memory. For large files, this can cause severe problems that can culminate in the recycling of the ASP.NET process. The TransmitFile method is designed to elegantly work around the problem. It sends output directly from a file to the ASP.NET ISAPI extension and then down to the client, without passing a humongous string to the ISAPI extension.
Note
Although TransmitFile makes large file downloads more stable than ever and fixes the problem of recycling, it is far from being a full solution to the problem of tracking and resuming large file downloads. For example, if a download fails, for whatever reason, TransmitFile can start it again only from the beginning. The article found at the following Web site discusses a better approach to the problem: http://www.devx.com/dotnet/Article/22533.
The HttpRequest Object
The HttpRequest object groups all the information contained in the HTTP packet that represents the incoming Web request. The contents of the various HTTP headers, the query string, or the form’s input fields, path, and URL information are organized in a series of collections and other ad hoc objects for easy and effective programmatic access. The HttpRequest object is populated as soon as ASP.NET begins working on a Web request, and it’s made available through the Request property of HttpContext.
HttpRequest exposes a fair number of properties and is one of the objects that has been more significantly enriched in the transition from ASP to ASP.NET.
Properties of the HttpRequest Class
The class properties can be categorized into three groups based on the type of information they contain: the type of the request, client data, and connection.
Information About the Request
Table 16-12 lists the properties that define the type of request