[ASP.NET] ashx(泛型處理常式)中存取Session(工作階段變數)

原本的ashx

Imports System
<%@ WebHandler Language="VB" Class="Handler" %>

Imports System
Imports System.Web

Public Class Handler : Implements IHttpHandler
    
    Public Sub ProcessRequest(ByVal context As HttpContext) Implements IHttpHandler.ProcessRequest
        context.Response.ContentType = "text/plain"
        context.Response.Write("Hello World")
    End Sub
 
    Public ReadOnly Property IsReusable() As Boolean Implements IHttpHandler.IsReusable
        Get
            Return False
        End Get
    End Property

End Class

修改後的ashx
<%@ WebHandler Language="VB" Class="Handler" %>

Imports System
Imports System.Web

Public Class Handler : Implements IHttpHandler, System.Web.SessionState.IRequiresSessionState
    
    Public Sub ProcessRequest(ByVal context As HttpContext) Implements IHttpHandler.ProcessRequest
        context.Response.ContentType = "text/plain"
        context.Response.Write("Hello World")
        If HttpContext.Current.Session.Item("SessionName") Is Nothing Then
            context.Response.Write("You Can Do Something.")
        End If
    End Sub
 
    Public ReadOnly Property IsReusable() As Boolean Implements IHttpHandler.IsReusable
        Get
            Return False
        End Get
    End Property

End Class
主要是加上System.Web.SessionState.IRequiresSessionSta 這句即可讀取

留言

這個網誌中的熱門文章

net use 系統發生 1219 錯誤

[MS SQL]資料庫複寫 刪除發行集 無法刪除

[Windows]密碼複雜性需求 停用密碼複雜度