简体   繁体   中英

Restart session in asp.net c# an json

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 tag to execute a Json and this ends the session, for this you must call the method RestartSesion ().

All code is in the MasterPage.

The problem is that only the alert displays a text '' Error; ''.

I tell you what I did.

I have a VSesion class that has several methods, but the one I am working on today is RestartSesion.

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

Template.Master.cs calls ReiniciarSesion .

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

Template.Master. Here is my trouble.

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

Here is the HTML template:

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

<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. Thank you.!!

As @JeroenHeier noted, Template.Master/ReiniciarSession points to a master page, and those are used by actual .aspx pages to render, they're not browsable. You have to implement the ReiniciarSession WebMethod in a specific page, like Login , instead of the Master page.

Then change the AJAX call to that page:

function ReiniciarSession() {
    $.ajax({
        ...
        url: "/Login.aspx/ReiniciarSession",
        ...
    });
}

As the session is independent of which page you're browsing, the effect will be the same.

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