Begin a new session in ASP.NET using C# and JSON

I am currently facing an issue in my ASP.NET C# project where I need to execute a Json by clicking on a tag, but this action ends the session. To resolve this, I have to call the method RestartSesion().

The code for this is located in the MasterPage. However, when I try to click on the tag, I only receive an alert with the text "Error."

Let me explain what I have done so far. I have a class named VSesion with multiple methods, and the one I am focusing on right now is RestartSesion.

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

The method ReiniciarSession is called in Template.Master.

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

However, the trouble arises in Template.Master. Here is the related script:

<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>

Below 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 would greatly appreciate any assistance or insights on this issue. Thank you!.

Answer №1

@JeroenHeier made a crucial point when discussing the issue involving Template.Master/ReiniciarSession. This points to a master page, which is utilized by actual .aspx pages for rendering purposes. However, they are not directly accessible through browsing. The recommended approach is to implement the ReiniciarSession WebMethod on a specific page, such as Login, rather than on the Master page.

Subsequently, ensure that the AJAX call is directed towards that particular page:

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

It's important to note that since the session is separate from the page being viewed, making this adjustment will produce the same desired outcome.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Tips for setting a date format to dd-mm-yyyy in Internet Explorer to match the one displayed in Google Chrome

Since type=date does not work in IE, is there a way to achieve the dd-mm-yyyy format like in other browsers? Any suggestions would be greatly appreciated. Thanks! ...

Is it possible for me to run two processes simultaneously on the same server - one to handle incoming requests and another to continuously poll a data storage system?

I am currently in the process of developing a server with two main functions: An API that can create, update, retrieve, and delete data in a local storage (similar to managing files on the server for a project prototype) A continuous loop that monito ...

Struggling to make multiple updates to a scope

I am currently utilizing Angular in conjunction with the Ionic Framework beta 1. Below is the HTML code for my ng-repeat: <a href="{{item.url}}" class="item item-avatar" ng-repeat="item in restocks | reverse" ng-if="!$first"> <img src="https: ...

Translate a jQuery ajax request utilizing jQuery().serialize into plain JavaScript

Currently, I've been in the process of converting a jQuery script into vanilla JavaScript to completely eliminate the need for jQuery. The main functionality of the code includes: Upon clicking a button on the front end, an ajax request is sent, upda ...

Getting pictures dynamically from the backend with unspecified file types

Greetings to my fellow Stackoverflow-Users, Lately, I was tasked with the requirement of loading images dynamically from the backend into my application. Up until now, it was always assumed that we would only be dealing with SVG images since there was no ...

Implementing advanced checkbox filtering feature in React

Does anyone have experience with creating dynamic Checkbox filtering in React using Material-UI? I'm finding it challenging because the checkbox options are generated dynamically from incoming data and need to be categorized by type of Select componen ...

Sliding out list elements with jQuery ladder effect

I'm stuck on a seemingly simple task and need some guidance. facepalm. Currently, my site at this link has a menu list item issue where they all come out from the left side after the picture loads. I want them to slide out one by one, starting from t ...

Sharing Master Pages across projects is a breeze with this easy guide

I am currently working on a web project and I am in need of sharing a master page to ensure that each new page I create follows the same basic layout. My initial thought was to create a project with this master page and then add it as a reference in each w ...

The execution of the Javascript function is not being carried out

Why is alert("Test1") being executed, but alert("Test2") is not running? P.S. I'm currently not utilizing JSON data. <script> $(document).ready(function() { var param = 10; $.getJSON( 'modules/mod_sche ...

The issue with property unicode malfunctioning in bootstrap was encountered

I have tried to find an answer for this question and looked into this source but unfortunately, when I copy the code into my project, it doesn't display Unicode characters. section { padding: 60px 0; } section .section-title { color: #0d2d3e ...

The error message "Getting undefined method map for string for the json parameter" occurs when calling `http_request.set_form

I encountered an issue when attempting to pass a JSON representation to a set_form_data method, resulting in the following error: undefined method `map' for "{\"first_name\":\"bill\",\"last_name\":\"gates\"}":S ...

Navigate through intricate nested JSON array structures in JavaScript

nested json structure Json Tree Structure: { "id": "30080", "dataelements": { "Name": "abc" }, "children": [ { "id": "33024", "dataelements": { "Name": "a" }, "children": [ { "id": "33024", ...

Tips for utilizing JavaScript to engage with a Cisco call manager

Our team is currently working on an IVR web application built with node js. I am wondering if it is feasible to integrate with the cisco unified call manager directly through node js in our web application? ...

Invoke the javascript function by referencing the JavaScript file

I'm encountering an issue with two JavaScript files. One file uses the prototype syntax, while the other utilizes jQuery. Unfortunately, they don't seem to work harmoniously together. I've attempted calling the functions within the files usi ...

Choose (disregard if unavailable) for querying JSON logs in Spark SQL

Recently, I've started working with Apache Spark and conducting various Proof of Concepts (POCs). Currently, I'm exploring the process of reading structured JSON logs, where some fields may not always be present. For instance: { "item": "A", ...

Unable to construct React/Next project - identified page lacking a React Component as default export (context api file)

When attempting to compile my Next.js project, I encountered an error message in the terminal: Error: Build optimization failed: found page without a React Component as default export in pages/components/context/Context The file in question is utilizing ...

What is the best way to add an array to my JSON object in Javascript?

I'm currently in the process of formatting an array into a JSON object for API submission. Struggling to find the right method to transform my array into the desired structure. This is what my array looks like: data: [ ["Lisa", "Heinz", "1993-04 ...

applying multiple conditions to filter data in javascript

I have been working on filtering data based on input, and it is functioning well. However, I am looking to add an additional condition to the filter. I want it to return if either the title or another value (such as sendFrom) is provided. const newData = ...

Placing buttons on top of a video within a div container

I am facing a challenge with implementing custom controls on a video embedded in my webpage. I have tried setting a div to absolute position for the buttons, but they are not clickable. Is there a way to achieve this without using jQuery? I need the butto ...

AngularJS SmartyUI position not dynamically updating on SmartyStreets plugin

In my angularJS application, I am utilizing SmartyStreets' LiveAddress for address validation and autofilling two fields in a form. After the user submits the form, a message (either success or failure) is displayed in a div above the form. However, t ...