Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I posted the original question for this and I think I finally figure out a solution for it. While its not as elegant as I had hoped, it works (which is more than I have found so far). The solution lies around the OnBegin AjaxOption and some Jquery. My ActionLink (it could also be an Ajax.BeginForm that this is applied to) looks like this</p> <pre><code>&lt;%= Ajax.ActionLink("Do Ajax Function", "AjaxAction", new { }, new AjaxOptions { OnBegin="check_session", OnSuccess = "ajaxSuccess", UpdateTargetId = "update_container"}, new { })%&gt; </code></pre> <p>I have a controller action in my Account controller that checks session, in my case I am storing the user object so i check to see if it is null, and returns 'True' or 'False' as content. The OutputCache is important otherwise your results will be cached.</p> <pre><code>[OutputCache(Duration=0, VaryByParam="None")] public ActionResult CheckAjaxSessionExpired() { bool _sessionExpired = false; if (Session["User"] == null) { _sessionExpired = true; } return Content(_sessionExpired.ToString()); } </code></pre> <p>The last part is the <code>check_session</code> function specified as the OnBegin handler. The Jquery ajax call hits the session check action and checks the response. If the response is 'True' (meaning the session has expired), it sets the location of the page back to the home page (or google in this case).</p> <pre><code>function check_session(){ var url = '&lt;%= Url.Action("CheckAjaxSessionExpired", "Account") %&gt;'; //GET CHECK SESSION $.ajax({ url: url, success: function(data) { //alert(data); if (data == "True"){ document.location = "http://www.google.com"; } }, error: function() { alert("there was a problem loading the ajax session check"); } }); } </code></pre> <p>There are several things to remember if using this method. The first is that this will run asynchronously. If you have data sensitive code that you are calling you will still need to perform a server side check on session. This code is intended to simply redirect to the login page (or home page) if the session is expired during an ajax call. The second thing is that the <code>check_session</code> function will need to be wired into the <code>OnBegin</code> event of all of your Ajax calls. If anyone has any tips or suggestions for improving this method, I would love to hear them. I hope this helps somebody.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload