简体   繁体   中英

Passing data from .aspx file to .ashx file using Session

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:-

  1. Is there any way to send data from .aspx to .ashx file??

  2. 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#

In the aspx you're adding Session["MId"] . In the ashx you're reading Session["MailId"] .

Make the keys you're using the same. (ie either MId or MailId , but not both).

Would suggest you define a constant to define this value since it's shared, then you can avoid such problems.

This code worked for me well

protected void Button1_Click(object sender, EventArgs e)
    {
        Session["MailId"] = "somemail@address.com";
        Response.Redirect("Handler.ashx");
    }

/// Code in ashx

public class Handler : IHttpHandler, IRequiresSessionState
{

    public void ProcessRequest (HttpContext context)
    {

        string sessionValue = context.Session["MailId"].ToString();
        context.Response.Write(sessionValue);
    }

    public bool IsReusable {
        get {
            return false;
        }
    }

}

I like to know whether u accessing handler from an ajax call or not

Now it's working.I did these changes:-

Instead of

[WebMethod(enableSession:true)] 

I put

[WebMethod(EnableSession = true)]

But Both are the correct way.

And I include async: false in my ajax call. (I think before setting the session it was trying to read it)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM