简体   繁体   English

在ASP.NET C#JSON中重新启动会话

[英]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 (). 我希望您能在这个主题上为我提供帮助,我正在使用ASP.NET C#开发一个项目,问题是我需要单击一个标签来执行Json,从而结束了该会话,为此,您必须调用方法RestartSesion()。

All code is in the MasterPage. 所有代码都在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. 我有一个VSesion类,它具有几种方法,但是我今天正在研究的方法是RestartSesion。

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

Template.Master.cs calls ReiniciarSesion . Template.Master.cs调用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: 这是HTML模板:

<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. 正如@JeroenHeier所指出的, Template.Master/ReiniciarSession指向母版页,而实际的.aspx页使用它们来呈现,因此无法浏览。 You have to implement the ReiniciarSession WebMethod in a specific page, like Login , instead of the Master page. 您必须在特定页面(例如Login )而不是Master页面中实现ReiniciarSession WebMethod

Then change the AJAX call to that page: 然后将AJAX调用更改为该页面:

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

As the session is independent of which page you're browsing, the effect will be the same. 由于会话与您正在浏览的页面无关,因此效果是相同的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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