实际上,我正在尝试将数据从 .aspx 发送到 .ashx 文件。我在 .aspx 中使用 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";
}
我正在字符串 session 中获取值。
.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 文件中显示 null 为什么? (对于特殊情况,我们可以使用/编写 if 条件,但我已经检查过字符串 session 是否有值(value)。
我是不是错过了什么??谢谢
这些是我已经使用过的链接:-
How to access Session in .ashx file?
Accessing context session variables in c#
最佳答案
在您要添加 Session["MId"]
的 aspx 中。在 ashx 中,您正在阅读 Session["MailId"]
。
使您使用的键相同。 (即 MId
或 MailId
,但不能同时使用)。
建议你定义一个常量来定义这个值,因为它是共享的,这样你就可以避免这样的问题。
关于c# - 使用 session 将数据从 .aspx 文件传递到 .ashx 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20901714/