首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式碼之家 >> 編程語言 >  >> .NET 網頁程式設計 >> ASP.NET 基礎 >> ASP.NET 基礎

ASHX 泛型處理程序無法訪問 Session 的解決方案:實現 IRequiresSessionState

     編輯:ASP.NET 基礎

問題現象

.ashx 處理程序中 context.Session 為 null,拋出 NullReferenceException,無法讀寫 Session 數據。

根本原因

默認 IHttpHandler 不支持 Session,需顯式實現標記接口聲明需求。

解決方案:實現兩個標記接口

using System.Web.SessionState;

public class MyHandler : IHttpHandler, IRequiresSessionState, IReadOnlySessionState
{
    public void ProcessRequest(HttpContext context)
    {
        // 現在可以正常訪問 Session
        int userId = (int)context.Session["UserId"];
        string userName = context.Session["UserName"]?.ToString();
        
        // 業務邏輯...
        context.Response.ContentType = "application/json";
        context.Response.Write("{\"success\":true}");
    }
    
    public bool IsReusable => false;
}

接口作用對照

接口作用場景
IRequiresSessionState讀寫 Session(獲取鎖)需修改 Session 數據
IReadOnlySessionState只讀 Session(無鎖,性能更好)僅讀取 Session 數據

完整示例:AJAX 處理器

<%@ WebHandler Language="C#" Class="ProductHandler" %>
using System;
using System.Web;
using System.Web.SessionState;
using Newtonsoft.Json;

public class ProductHandler : IHttpHandler, IRequiresSessionState
{
    public void ProcessRequest(HttpContext context)
    {
        context.Response.ContentType = "application/json";
        
        string action = context.Request["action"];
        string result = "";
        
        try
        {
            switch (action)
            {
                case "getList":
                    // 讀取 Session 中的公司 ID
                    int companyId = (int)context.Session["CompanyId"];
                    var list = ProductService.GetList(companyId);
                    result = JsonConvert.SerializeObject(new { success = true, data = list });
                    break;
                    
                case "save":
                    // 寫入 Session 記錄操作日誌
                    var log = new OperationLog 
                    { 
                        UserId = (int)context.Session["UserId"],
                        Action = "SaveProduct",
                        Time = DateTime.Now 
                    };
                    context.Session["LastLog"] = log;
                    result = "{\"success\":true}";
                    break;
            }
        }
        catch (Exception ex)
        {
            result = $"{{\"success\":false,\"msg\":\"{ex.Message}\"}}";
        }
        
        context.Response.Write(result);
    }
    
    public bool IsReusable => false;
}

常見誤區

  1. 只實現 IRequiresSessionState:若僅讀取,建議改用 IReadOnlySessionState 避免鎖競爭
  2. Session 為 null:檢查 web.config <sessionState mode="InProc"> 配置正確
  3. 無狀態負載均衡:多服務器需配置 mode="StateServer" 或 "SQLServer" 共享 Session

ASP.NET Core 對應方案

// Startup.cs
services.AddSession(options => 
{
    options.IdleTimeout = TimeSpan.FromMinutes(30);
    options.Cookie.HttpOnly = true;
});

// Middleware 順序重要
app.UseSession();

// Handler/Controller 中
HttpContext.Session.SetInt32("UserId", 123);
int? userId = HttpContext.Session.GetInt32("UserId");

一行接口聲明解決 ASHX Session 無法訪問問題,理解背後機制才能舉一反三。

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式碼之家 All Rights Reserved