Have you ever noticed that some pages on Microsoft's web site have an extension of .ASHX (among other things)?
ASHX files are WebHandlers that derive from IHttpHandler. Their advantages are mainly that you can gain access to the HttpContext without all the overhead of loading the Page class if you don't need the UI.
These classes have only two required methods - ProcessRequest, which accepts an HttpContext parameter, and the boolean IsReusable. In short, ASHX files let you create an HTTP endpoint that can return any content type that you want. And, with ASHX, there is no requirement for registration in web.config or machine.config - you just drop them into your project and they are usable "out of the box".
One thing I've noticed is that the C# guys seem to have all sorts of articles on using ASHX files, but the VB.NET guys do not. So, after a little bit of research, I figured-out how to write an ASHX page in VB.NET. The code sample below only has the small function of logging a user out of a web site... Maybe it's not the best example, but its all I've got!
1 <%@ WebHandler Language="VB" Class="MyNamespace.Web.Logout" %>
2
3 Imports System.Web
4 Imports System.Web.Security
5
6 Namespace MyNamespace.Web
7 Public Class Logout
8 Implements IHttpHandler
9 Public Sub ProcessRequest(ByVal context As HttpContext) _
10 Implements IHttpHandler.ProcessRequest
11 FormsAuthentication.SignOut()
12 Context.Response.Redirect("default.aspx")
13 End Sub
14
15 Public ReadOnly Property IsReusable() As Boolean _
16 Implements IHttpHandler.IsReusable
17 Get
18 Return False
19 End Get
20 End Property
21 End Class
22 End Namespace
Using an ASHX page is just the tip-of-the-iceberg when it comes to using HTTP Handlers. Scott Mitchell's article, “Serving Dynamic Content with HTTP Handlers“ shows how HTTP Handlers can be used to format code on web pages, prevent people from “hot-linking” to images on your web site, and re-writing URLs to more user-friendly names.
Using ASHX and Code-Behind
The method described above shows how to use an ASHX as a single file. However, VisualStudio.NET doesn't recognized the file type, hence no Intellisense support :( I don't know about you, but I can't live without Intellisense, and there is hope! An ASHX file can have a code-behind class file just like an ASPX page. The actual ASHX page should only have a line like the following in it:
<%@ WebHandler Language="VB" Codebehind="Logout.ashx.vb"
Class="GMR.eFelix.Web.Logout" %>
The ASHX code-behind page would be a normal class file.