获取会话值 - ASP.NET Web Handler文件

问题描述 投票:2回答:1

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

但我仍然无法获得会话价值。请让我得到你的建议。

[ASPX设计文件]

...
<form id="form1" runat="server">
<div>
    <iframe runat="server" id="iframepdf" height="600" width="800" src="./PDFViewer.ashx"></iframe>
</div>
</form>
...

[ASPX代码落后]

...
namespace WebApplication1
{
public partial class NameCard : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {   
        Session["PDFFileName"] = @"D:\Test.pdf";
    }
}
}

当你看到我的代码背后的文件, 我为session对象设置了一些值。 然后,我写下ashx文件。

[ASHX Code Behind]

...
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;
        }
    }
}
}
c# asp.net session
1个回答
0
投票

从阅读你的代码中的评论我解释它你得到context.Session["StackOverflow"]而不是["PDFFileName"]值?那是对的吗?在分配会话值的页面中以及在使用它们的处理程序中放置断点并运行调试器。

也许你拼错了钥匙?请记住,密钥区分大小写。

© www.soinside.com 2019 - 2024. All rights reserved.