簡體   English   中英

無法訪問 ASHX 處理程序文件中的會話變量

[英]Cannot access session variables in ASHX handler file

我有一個用於遺留目的的 ASHX 處理程序文件(我們有一個無法更新的 win32 應用程序,它引用了一個 ASHX 處理程序)

以下是處理程序,它似乎工作正常,但它不會訪問在 MVC 應用程序中其他地方設置的會話變量:

using Core.Helpers;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Script.Serialization;
using System.Web.SessionState;

namespace handlers
{
    /// <summary>
    /// Summary description for hShowChosenTicketsFGL
    /// </summary>
    public class hShowChosenTicketsFGL : IHttpHandler, IRequiresSessionState,  IReadOnlySessionState
    {
        public long orderId;

        public void ProcessRequest(HttpContext context)
        {
            List<string> response = new List<string>();

            if (context.Session["OrderId"] == null)
            orderId = -1;
            else
                orderId = Convert.ToInt64(context.Session["OrderId"]);

            if (orderId == -1)
                orderId = Convert.ToInt64(context.Request.QueryString["OrderId"]);

            bool ifReturns = false;
            if (orderId.ToString().StartsWith("1111"))
                ifReturns = true;

            if (ifReturns)
                orderId = long.Parse(orderId.ToString().Substring(4));

            JavaScriptSerializer json = new JavaScriptSerializer();
            var fgl = new List<string>();

            if(HttpContext.Current.Session["ReceiptFGL"] != null)
            {
                var text = HttpContext.Current.Session["ReceiptFGL"].ToString();
                fgl.Add(text);


                HttpContext.Current.Session["ReceiptFGL"] = null;
            }

            var jsonReponse = json.Serialize(fgl);
            context.Response.Write(jsonReponse);
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
       }
   }
}

我也嘗試過路由方法,但無法使其正常工作,應用程序不斷返回並說找不到位置。 這是我使用的路線:

routes.MapRoute(
    name: "LegacyTicketGet",
    url: "handlers/hShowChosenTicketsFGL.ashx",
    defaults: new { controller = "API", action = "GetReceiptFGL" }
);

有沒有人知道我可能做錯了什么,或者我如何放棄處理程序並使用路由來路由到 JsonResult 操作?

這對我有用。 我遇到過同樣的問題。

確保您從 System.Web.SessionState.IRequiresSessionState 繼承,因此您的 ashx 文件類應如下所示。

public class ImageHandler : IHttpHandler, System.Web.SessionState.IRequiresSessionState
        {
            public void ProcessRequest(HttpContext context)
            {
              string Name = "";
              if (context.Session["mysessionvar"] != null)
                 Name = context.Session["mysessionvar"].ToString();
            }
        }

僅調用 localhost 可能不起作用。 肯定適用於 IIS 設置。

參考https://thedeveloperblog.com

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM