The Built-in Handlers
You've been dealing with HttpHandlers all along, even if you didn't realize it. In Chapter 6, "The Nuts and Bolts of IIS and Web Applications," we explained that a number of different file extensions are mapped in the IIS control panel to be handled by ASP.NET. What the control panel doesn't show is how ASP.NET deals with the requests that land in its lap. To understand that, we'll have to take a look at the machine.config file, normally located in the C:\WINDOWS\Microsoft.NET\Framework\v2.0.xxxx\CONFIG folder.
You're probably already familiar with the web.config file found in your applications, which sets up the configuration for the application. The machine.config file can store much of the same configuration data, except that it applies to the entire machine. |
Listing 8.1. The httpHandlers section of machine.config
[View full width]
<httpHandlers>
<clear />
<add verb="*" path="trace.axd" type="System.Web.Handlers
.TraceHandler" />
<add verb="GET" path="WebAdmin.axd" type="System.Web
.Handlers.WebAdminHandler" />
<add verb="GET" path="WebResource.axd" type="System.Web
.Handlers.AssemblyResourceLoader" />
<add verb="GET" path="CachedImageService.axd" type="System.Web.UI.Imaging.CachedImageServiceHandler" />
<add verb="GET" path="counters.axd" type="System.Web.Handlers.SiteCountersHandler" />
<add verb="GET" path="precompile.axd" type="System.Web.Handlers.PrecompHandler" />
<add verb="GET" path="WebPartExport.axd" type="System.Web.Handlers.WebPartExportHandler" />
<add verb="*" path="*.aspx" type="System.Web.UI.PageHandlerFactory" />
<add verb="*" path="*.ashx" type="System.Web.UI.SimpleHandlerFactory" />
<add verb="*" path="*.asix" type="System.Web.UI.ImageGeneratorFactory" />
<add verb="*" path="*.asmx" type="System.Web.Services.Protocols.WebServiceHandlerFactory, System.Web.Services,
Version=2.0.3600.0, Culture=neutral,PublicKeyToken=b03f5f7f11d50a3a" validate="false" />
<add verb="*" path="*.rem" type="System.Runtime.Remoting.Channels.Http.HttpRemotingHandlerFactory, System.Runtime.Remoting
, Version=2.0.3600.0, Culture=neutral,PublicKeyToken=b77a5c561934e089" validate="false" />
<add verb="*" path="*.soap" type="System.Runtime.Remoting.Channels.Http.HttpRemotingHandlerFactory, System.Runtime.Remoting,
Version=2.0.3600.0, Culture=neutral,PublicKeyToken=b77a5c561934e089" validate="false" />
<add verb="*" path="*.asax" type="System.Web.HttpForbiddenHandler" />
<add verb="*" path="*.ascx" type="System.Web.HttpForbiddenHandler" />
<add verb="*" path="*.master" type="System.Web.HttpForbiddenHandler" />
<add verb="*" path="*.skin" type="System.Web.HttpForbiddenHandler" />
<add verb="*" path="*.browser" type="System.Web.HttpForbiddenHandler" />
<add verb="*" path="*.sitemap" type="System.Web.HttpForbiddenHandler" />
<add verb="GET,HEAD" path="*.dll.config" type="System.Web.StaticFileHandler" />
<add verb="GET,HEAD" path="*.exe.config" type="System.Web.StaticFileHandler" />
<add verb="*" path="*.config" type="System.Web.HttpForbiddenHandler" />
<add verb="*" path="*.cs" type="System.Web.HttpForbiddenHandler" />
<add verb="*" path="*.csproj" type="System.Web.HttpForbiddenHandler" />
<add verb="*" path="*.vb" type="System.Web.HttpForbiddenHandler" />
<add verb="*" path="*.vbproj" type="System.Web.HttpForbiddenHandler" />
<add verb="*" path="*.webinfo" type="System.Web.HttpForbiddenHandler" />
<add verb="*" path="*.licx" type="System.Web.HttpForbiddenHandler" />
<add verb="*" path="*.resx" type="System.Web.HttpForbiddenHandler" />
<add verb="*" path="*.resources" type="System.Web.HttpForbiddenHandler" />
<add verb="*" path="*.mdb" type="System.Web.HttpForbiddenHandler" />
<add verb="*" path="*.vjsproj" type="System.Web.HttpForbiddenHandler" />
<add verb="*" path="*.java" type="System.Web.HttpForbiddenHandler" />
<add verb="*" path="*.jsl" type="System.Web.HttpForbiddenHandler" />
<add verb="*" path="*.ldb" type="System.Web.HttpForbiddenHandler" />
<add verb="*" path="*.mdw" type="System.Web.HttpForbiddenHandler" />
<add verb="GET,HEAD,POST" path="*" type="System.Web.DefaultHttpHandler" />
<add verb="*" path="*" type="System.Web.HttpMethodNotAllowedHandler" />
</httpHandlers>
Listing 8.1 shows the httpHandlers section of machine.config, and many of the file extensions there should seem familiar to you. This section of the configuration file maps file extensions to a particular handler for processing. Our good friend the .aspx extension is handled by a class called System.Web.UI.PageHandlerFactory. Files that the average Internet user should not be allowed to touch, such as .cs and .vb files, are handled by System.Web.HttpForbiddenHandler, which generates an error message for the user. We certainly wouldn't want users seeing our code!