RAK
RAK

Reputation: 283

Accessing Session Variable in Global.asax

In my ASP.net Web Forms Application, I was trying to reset the Session timeout on every ajax request. I understand that it can be done by implementing the EndRequest Method in Global.asax as below:

  Sub Application_EndRequest(sender As Object, ByVal e As EventArgs)

  End Sub

But the problem is that Session Variable is not in scope here. I want to execute the following code:

 If (Session("_UserID")) Is Nothing Then
    Session.Timeout = 60 * 24 '24 hours
End If

Any advise in this regard is higlhly appreciated.

Upvotes: 0

Views: 1231

Answers (2)

Gagan Deep
Gagan Deep

Reputation: 1509

Take a Look at this. i believe this could solve your problem.

$(document).ready(function () {         



        $("#asp").on("click", function () {
            var obj = {};                
            $.ajax({
                type: "POST",
                url: "Default7.aspx/getname1",
                data: JSON.stringify(obj),
                contentType: "application/json; charset=utf-8",
                dataType: "json",
                global :true,
                success: function (response) {
                    alert(response.d);
                },
                failure: function (response) {
                    alert("faillure");
                }
            });
            return false;

        });


        $("#asp1").on("click", function () {
            var obj = {};
            $.ajax({
                type: "POST",
                url: "Default7.aspx/getname2",
                data: JSON.stringify(obj),
                contentType: "application/json; charset=utf-8",
                dataType: "json",
                global: true,
                success: function (response) {
                    alert(response.d);
                },
                failure: function (response) {
                    alert("faillure");
                }
            });
            return false;

        });
    });


    $(document).ajaxComplete(function () {
        var obj = {};

        $.ajax({
            type: "POST",
            url: "Default7.aspx/getname1",
            data: JSON.stringify(obj),
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            global :false,
            success: function (response) {
                alert(response.d);
            },
            failure: function (response) {
                alert("faillure");
            }
        });
        return false;
    });




<button id="asp" >Click Me</button>
<button id="asp1" >Click Me</button>

Do whatever you want in getname1 function. it will be called each time you make an ajax call to the page.But in your case problem remains that you need to call a method of the Master Page. Unfortunately i think it is not possible to call web methods of Master Page through ajax call as Master Pages are converted to user controls at runtime. so what can be done it to create a Web Service(.asmx) file and add a WebMethod inside it and call it with .ajax call as suggested above in the ajaxComplete Method. Web Service Method would look like this.

 [WebMethod(EnableSession = true)]
 public string setSession()
 {
    HttpContext.Current.Session.TimeOut=60.24;
    return "1";
 }

Upvotes: 0

Gagan Deep
Gagan Deep

Reputation: 1509

try this in Global.asax. this event fires for each request.

void Application_AcquireRequestState(object sender, EventArgs e)
{
    // Session 
    HttpContext context = HttpContext.Current;
    context.Session.Timeout = 60 * 24;
}

Upvotes: 1

Related Questions