在 ashx 和 aspx 之间共享会话 [英] Share session between ashx and aspx

查看:72
本文介绍了在 ashx 和 aspx 之间共享会话的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 ashx 文件(实现 IRequiresSessionState 接口)将值存储到会话状态,如下所示:

I have an ashx file (implementing the IRequiresSessionState interface) storing a value to session state like so:

context.Session.Add("answer", _answer);

在触发 ashx 调用的 aspx 文件的代码隐藏中,我尝试像这样访问会话变量:

In the code-behind of the aspx file which triggers the ashx call I try to access the session variable like so:

Context.Session["answer"].ToString()

然后我得到一个 NullReferenceException.

and I get a NullReferenceException.

在这种情况下,我需要如何从会话状态开始写入和读取?

How do I need to go about writing and reading from session state in this context?

推荐答案

我怀疑问题出在 url 名称和会话上的 cookie 设置上,因为如果您放置了 IRequiresSessionState 接口,您应该读取会话.

I susspect that the issue is on url name and in the setup of your cookies on the session, because if you have place the IRequiresSessionState interface you sould read the session.

转到您的 web.config 并将您在 httpCookies 上的域名设置为您的域名称,不带www".

Go to your web.config and set your domain name on the httpCookies as your name of your domain with out the 'www'.

<httpCookies domain="domainname.com" .... />

通过这种方式,您的页面会读取 cookie 以及您从 domainname.com、ether www.domainname.com 调用它的会话.

This way your page reads the cookie and the session ether you call it from domainname.com, ether www.domainname.com.

这篇关于在 ashx 和 aspx 之间共享会话的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆