【发布时间】:2014-09-25 14:41:37
【问题描述】:
我有一个 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 操作?
【问题讨论】:
-
不确定,但服务器 (IIS) 可能在您的应用程序获得成功之前抓取了 ashx 请求。您可以尝试使用 IIS 重写规则将该 .ashx 请求定向到您选择的控制器。 ashx 和你的 mvc 应用在同一个应用中吗?
-
我假设您正在回答路线不起作用的问题?由于没有路由,处理程序文件被调用,因为我可以进入它,它只是不会获取我之前设置的会话变量。
-
是的,但我没有回答,因为我没有提供明确的解决方案。对于您的会话问题,我认为同一个应用程序中的 ashx 应该能够与同一个会话进行对话。
-
“我们有一个无法更新的 win32 应用程序引用了 ASHX 处理程序”——这是否意味着应用程序调用了处理程序并且不费心传递会话 cookie?
-
嗯,它是一个包装浏览器的win32应用程序。所以我假设浏览器正在处理会话 cookie,因为会话变量在我要移植到 MVC 的 WebForms 应用程序中使用的 ashx 处理程序中工作得非常好。
标签: c# asp.net asp.net-mvc asp.net-mvc-4 ashx