【问题标题】:get session value - ASP.NET Web Handler file获取会话值 - ASP.NET Web 处理程序文件
【发布时间】:2020-03-21 08:02:35
【问题描述】:

我无法在 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";
    }
}
}

正如您在文件后面看到的代码,
我为会话对象设置了一些值。
然后,我在 ashx 文件下面写了。

[ASHX 代码隐藏]

...
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;
        }
    }
}
}

【问题讨论】:

  • 你的 PageLoad 总是在你 ashx 之前被调用吗?
  • 当然,PageLoad 在加载 ashx 之前首先运行。

标签: c# asp.net session


【解决方案1】:

通过阅读您代码中的 cmets,我认为您获得了 context.Session["StackOverflow"] 而不是 ["PDFFileName"] 值?那是对的吗?在分配会话值的页面和使用它们并运行调试器的处理程序中放置断点。

也许您拼错了按键?请记住,键是区分大小写的。

【讨论】:

    猜你喜欢
    • 2011-06-20
    • 1970-01-01
    • 2012-05-13
    • 1970-01-01
    • 1970-01-01
    • 2013-04-26
    • 2011-11-28
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多