C# 获取会话值-ASP.NET Web处理程序文件

C# 获取会话值-ASP.NET Web处理程序文件,c#,asp.net,session,C#,Asp.net,Session,我无法在ASHX文件中获取会话值。 我已经搜索过了,所以我可以解决我的问题。 但我仍然无法获取会话值。 请让我听听你的建议 [ASPX设计文件] ... <form id="form1" runat="server"> <div> <iframe runat="server" id="iframepdf" height="600" width="800" src="./PDFViewer.ashx"></iframe> </div&g

我无法在ASHX文件中获取会话值。
我已经搜索过了,所以我可以解决我的问题。

但我仍然无法获取会话值。 请让我听听你的建议

[ASPX设计文件]

...
<form id="form1" runat="server">
<div>
    <iframe runat="server" id="iframepdf" height="600" width="800" src="./PDFViewer.ashx"></iframe>
</div>
</form>
...
正如您看到我的代码隐藏文件一样,
我为会话对象设置了一些值。
然后,我在下面写下ashx文件

[ASHX代码隐藏]

...
namespace WebApplication1
{
public partial class NameCard : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {   
        Session["PDFFileName"] = @"D:\Test.pdf";
    }
}
}
...
namespace WebApplication1
{
public class PDFViewer : IHttpHandler, System.Web.SessionState.IRequiresSessionState
{
    public void ProcessRequest(HttpContext context)
    {   
        context.Session["StackOverflow"] = "overflowing";  // This one give me output value as "overflowing"
        if (context.Session["PDFFileName"] != null) {  // My problem is this line, It always say null value ...
            context.Response.ContentType = "Application/pdf";
            var PDFFileName = context.Session["PDFFileName"].ToString();                
            context.Response.WriteFile(PDFFileName);
            context.Response.End();
            context.Session["PDFFileName"] = string.Empty;
        }

    }

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

通过阅读代码中的注释,我解释为您得到的是
context.Session[“StackOverflow”]
,而不是
[“PDFFileName”]
值?对吗?在分配会话值的页面和使用会话值并运行调试器的处理程序中放置断点


也许你把钥匙拼错了?请记住,键是区分大小写的。

是否总是在ashx之前调用PageLoad?当然,在ashx加载之前先运行PageLoad。