实际上,我正在尝试从.aspx文件发送数据到.ashx文件。我正在使用.asps中的Session,并尝试在.ashx中获取session的值,但是它显示异常:对象引用未设置为对象的实例。
这是我的代码:
.aspx代码
[WebMethod(enableSession:true)]
public static string SetId(string Id)
{
HttpContext.Current.Session["MId"] = Id.ToString(); Console.Write(" ");
string session = HttpContext.Current.Session["MId"].ToString(); // Here I am getting value
//string session = HttpContext.Current.Session["MId"].ToString();
//BatchReadEmails.EmailProperties session = new BatchReadEmails.EmailProperties();
//session.MailId = Id.ToString();
return "Ok";
}
我正在从字符串会话中获取值。
.ashx 代码:
public class ChangeLogHandler : IHttpHandler, System.Web.SessionState.IRequiresSessionState
{
public void ProcessRequest(HttpContext context)
{
HttpRequest request = context.Request;
HttpResponse response = context.Response;
string session = "";
if (context.Session["MId"] != null)
session = context.Session["MId"].ToString();
else
session = "Hello India";
}
}
这里显示的是 session = "Hello India"
我的问题:
有没有办法从 .aspx 文件发送数据到 .ashx 文件?
我查了很多链接,都使用 if 判断是否为 null,但我已经在 .aspx 文件中检查过,它显示有值,但在 .ashx 文件中却显示为空。为什么?(对于特殊情况,我们可以使用 if 条件语句,但我已经检查过字符串 session 有值)。
我有什么遗漏吗?谢谢。
这些是我已经使用过的链接: