如何在HttpHandler中使用Session变量

7 .net httphandler

目标:我在内存中有一个缩略图作为字节数组.一旦用户上传了他们的图像,我想在将其写入数据库之前将其显示在httphandler中.我已经使用此代码成功读取并显示数据库.但现在我想从会话中显示它:

Public Sub ProcessRequest(ByVal context As HttpContext) _
    Implements IHttpHandler.ProcessRequest

    Dim oPhotoMgt As New PhotoMgt
    Dim intPhotoID As Int32 = context.Request.QueryString("id") 
    Dim oPhoto As New Photo
    oPhoto = oPhotoMgt.GetPhotoByID(intPhotoID)   

    context.Response.ContentType = "image/jpeg" 
    context.Response.BinaryWrite(oPhoto.Bytes.ToArray())
End Sub
Run Code Online (Sandbox Code Playgroud)

Tho*_*rin 15

您应该使用IRequiresSessionState接口(System.Web.SessionState命名空间)标记您的类.它没有方法或属性,因此您不必更改有关代码的任何其他内容.

签名将是:

Imports System.Web
Imports System.Web.SessionState

Public Class MyHandler
    Implements IHttpHandler, IRequiresSessionState

    Public Sub ProcessRequest(ByVal context As HttpContext) _
        Implements IHttpHandler.ProcessRequest

        context.Session("foo") = "bar"
    End Sub
End Class
Run Code Online (Sandbox Code Playgroud)