Actually I am trying to send data from .aspx to .ashx file. I am using Session in .aspx and trying to get value of session in .ashx but it is showing exception:: Object reference not set to an instance of an object.
Here is my code :-
.aspx code
[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";
}
I am getting value in string session.
.ashx code:-
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";
}
}
Here it is showing session = "Hello India"
My Question:-
Is there any way to send data from .aspx to .ashx file??
I checked so many links all are using if for null but I already check in .aspx file it is showing value there but showing null in .ashx file Why?? (For exceptional cases we can use/ write if condition but I already checked string session has value.
Am I missing something?? Thanks
These are the links I already used:-
How to access Session in .ashx file?
Accessing context session variables in c#