Restart Session on asp.net and json

0

I hope you can help me with this topic, I am working on a project with asp.net c #, the problem is that I need to click on a label to execute a Json and it ends the session.

I explain to you what I did.

I have a VSesion class that has several methods, but the one that concerns me today is RestartSession.

public static void ReiniciarSesion()
{
     HttpContext.Current.Session.Abandon();        
     HttpContext.Current.Response.Redirect(Resources.SitePages.Login);
}

In my Template.Master.cs I call the previous method in this way.

[WebMethod]
public static void ReiniciarSession()
{
     VSesion.ReiniciarSesion();
}

In the Template.Master I call it this way..this is where I have the problem.

<script language="javascript" type="text/javascript">
        $(document).ready(function() {
            $("#Reiniciar").click("click", function() {
                ReiniciarSession();
            });
        });
        function ReiniciarSession() {
            $.ajax({
                type: "POST",
                url: "TemplateSae.Master/ReiniciarSession",
                data: {},
                contentType: "application/json; charset=utf-8",
                dataType: "json",
                async: true,
                error: function(XMLHttpRequest, textStatus, errorThrown) {
                    alert(textStatus + ": " + XMLHttpRequest.responseText);
                }
            });
        }
    </script>

Here the label

<ul>
    <li>
      <a href="#" id="Reiniciar">
         <span>Salir</span>
      </a>  
    </li>
</ul>

By the way and I forgot I'm using

<script src="JS/jquery.js" type="text/javascript"></script>
<script src="JS/jquery-1.11.3.min.js" type="text/javascript"></script>

I hope you can help me. Thanks. !!

    
asked by Herlan Baringay Linares 23.11.2016 в 13:56
source

1 answer

1

The error is that when trying to access the HttpContext.Current.Session object, it has a null value.

To make the Session object available in a WebMethod, you must add the attribute [WebMethod(EnableSession = true)]

[WebMethod(EnableSession = true)]
public static void ReiniciarSession()
{
     VSesion.ReiniciarSesion();
}
    
answered by 13.12.2016 в 18:11