[英]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 设置。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.