Ti posto un handler fatto tempo fa in VB per visualizzare un prodotto:
Classe dell'handler che fa un contratto con IHttpHandler (cartella App_Code):
codice:
Imports Microsoft.VisualBasic
Imports system.Data
Imports System.Data.SqlClient
Imports System.Web
Imports System.Configuration
Public Class handler_prodotti
Implements IHttpHandler
Public ReadOnly Property IsReusable() As Boolean Implements System.Web.IHttpHandler.IsReusable
Get
Return False
End Get
End Property
Public Sub ProcessRequest(ByVal context As System.Web.HttpContext) Implements System.Web.IHttpHandler.ProcessRequest
Dim intidprodotto As Integer = ID_URL(context.Request.Path)
' Si connette al DB e riempie un datareader (dtr)
dtr = cmd.ExecuteReader(CommandBehavior.SingleRow)
If dtr.Read Then
context.Response.Write("
Descrizione del prodotto: ")
context.Response.Write("<font color=red>" & dtr("descrizione") & "</font>")
context.Response.Write("")
Else
context.Response.Write("
Non ci sono prodotti con questo ID !")
End If
context.Response.Write("
Pagina Iniziale ")
End Sub
Private Function ID_URL(ByVal URL_Barra As String) As Integer
Dim nomipagine() As String
Dim nomepagina As String
Dim strId As String
strId = String.Empty
nomipagine = URL_Barra.Split("/")
nomepagina = nomipagine(UBound(nomipagine))
For ind As Integer = 0 To nomepagina.Length - 1
If Char.IsDigit(nomepagina.Chars(ind)) Then
strId &= nomepagina.Chars(ind)
End If
Next
If Not IsNothing(strId) Then
Return CInt(strId)
Else : Return -1
End If
End Function
End Class
Web.Config:
codice:
<httpHandlers>
<add verb="*" path="prodotto*.aspx" type="[RootNameSpace].handler_prodotti, [AssemblyName]"></add>
</httpHandlers>
Dove [RootNameSpace] è lo spazio dei nomi di primo livello del tuo progetto, e [AssemblyName] è il nome dell'assembly principale.