Session returning null in WebMethod when hosted as Web Site on IIS

Arvind V 1 Reputation point
2022-12-15T20:09:07.26+00:00

I have an ASP.Net web apps application. Where I am using session to save a data. The application calls an external application where user can select a value and the selected value is stored in an external store and it returns a key to a return site which is an End.aspx page on our application.

I need to identify that I have received a key and then get the details and update the controls with value on the calling page. Since the End.aspx is independent page to let the calling page know that a key has been received I am saving the key in Session.

On the calling page.aspx, I have a timer pool that will call an ajax post to a WebMethod where I check if the session has the value to start processing, and then return true for the timer to stop.

All this works as expected when the site is hosted on IIS as Application on the 'Default Web Site', but when I host this site directly as a Website on IIS then the WebMethod has the session value empty. Why Hosting as a direct website is creating different sessions ids for the pages and in the WebMethod? But when hosting as Application on 'Default Web Site' it has same session ids for the two.

I am storing the session in SQL.

Below is the code I am using. In End.Aspx.cs

HttpContext.Current.Session["closed"] = true;  

In the Calling page.aspx.cs

[System.Web.Script.Services.ScriptMethod()]  
[System.Web.Services.WebMethod(EnableSession = true)]  
public static bool CheckClosed()  
{  
    bool closed = false;  
    if (HttpContext.Current.Session["closed"] != null)  
    {  
        HttpContext.Current.Session["closed"] = null;  
        return true;  
    }  
    return closed;  
}  

here is the JS that calls the WebMethod from the calling page.aspx

function GetIfClosed() {  
            $.ajax({  
                type: "POST",  
                url: '<%= ResolveUrl("callingpage.aspx/CheckClosed") %>',  
                data: "",  
                contentType: "application/json; charset=utf-8",  
                dataType: "json",  
                success: function (msg) {  
  
                    // Evaulate result..  
                    var process = msg.d  
  
                    if (process) {  
                        // destroy the timer to stop polling for progress  
                        clearTimeout(timer);  
                        $("#cboxClose").click();  
                        // Do your final message to the user here.                        
  
                    } else {  
  
                        // poll timer for another trip  
                        TimerPoll();  
                    }  
                }  
            });  
        }  
Windows development Internet Information Services
Developer technologies ASP.NET Other
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Anonymous
    2022-12-16T05:48:12.807+00:00

    Hi @Arvind V ,

    Unfortunately, I'm afraid I cannot reproduce the problem with the code you've provided. This is my test:

    End.aspx button click event:

       protected void setSession_Click(object sender, EventArgs e)  
               {  
                   HttpContext.Current.Session["closed"] = "something test..";  
                   Response.Redirect("/callingPage.aspx");  
               }  
    

    callingpage.aspx webmethod:

       [System.Web.Script.Services.ScriptMethod()]  
               [System.Web.Services.WebMethod(EnableSession = true)]  
               public static string CheckClosed()  
               {  
                   //bool closed = false;  
                   string sessionContent = string.Empty;  
                   if (HttpContext.Current.Session["closed"] != null)  
                   {  
                       sessionContent = HttpContext.Current.Session["closed"].ToString();  
                       HttpContext.Current.Session["closed"] = null;  
                       //return true;  
                   }  
                   return sessionContent;  
               }  
    

    Ajax method(print the result in console):

       $.ajax({  
               ......  
               success: function (msg) {  
                // Evaulate result..  
                 console.log(msg.d);  
               }  
          });  
    

    And finally deployed it on IIS like this:
    271245-image.png
    Here is the result screenshot:

    271301-image.png

    As you can see, it works fine. If possible, could you provide more relevant details? Perhaps you can try clearing your browser cache, as well as redeploying it and restarting IIS to see if the problem still occurs.

    Best regards,
    Xudong Peng


    If the answer is the right solution, please click "Accept Answer" and kindly upvote. If you have extra questions about this answer, please click "Comment".
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.