views:

22

answers:

1

In MVC, how can I properly handle a page time out in an AJAX Form. Currently when submitting an AJAX Form, the DOM of the AJAX form is replaced by the login page content. I would like to redirect the entire page to the Login page.

I have an ASP.Net MVC application with a Partial Form inside an Ajax form (AJAX.BeginForm). The application is using Windows Form Authentication. Normally when the website times-out, you are correctly redirected to the Login page. However when the AJAX Form submits and the website has timed-out, MVC returns the Login page as the content of the AJAX Form. The Controller action method is never reached. I have tried JQuery.load with the same result.

Controller

    public ActionResult Index()
    {
        ViewData["Now"] = DateTime.Now.ToString("HH:mm:ss");
        return View("Index");
    }
    public ActionResult AjaxTimeout()
    {
        ViewData["Now"] = DateTime.Now.ToString("HH:mm:ss");
        return PartialView("AjaxTimeout");
    }

Timeout.aspx

<%@ Page Language="C#" Inherits="System.Web.Mvc.ViewPage" %>
<html>
<head>
<script src="/Scripts/MicrosoftAjax.debug.js" type="text/javascript"></script>
<script src="/Scripts/MicrosoftMvcAjax.debug.js" type="text/javascript"></script>
</head>
<body>
    Parent page:
    <div id="ajaxtimeout" style="height:300px; width:300px;">
    <%= Html.Partial("AjaxTimeout") %>
</div>
</body>
</html>

AjaxTimeout.ascx

<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl" %>
<% using (Ajax.BeginForm("AjaxTimeout", new AjaxOptions { UpdateTargetId = "ajaxtimeout"})) { %>
    Return Time: <%= ViewData["Now"] %> 
    <input type="submit" value="Submit" />
<% } %>
+1  A: 

I don't use the Microsoft scripts for our apps, as we generally use jQuery for everything, so this solution will need to be investigated for your specific case, but here's how I solve it.

Whenever I have an ajax enabled app, I use a wrapper around jQuery's $.ajax function to make all my ajax requests with. Inside the wrapper method, before it returns the response to the code that made the ajax request, it specifically checks for a 401 response to see if the authentication timed out. If so, it just causes the whole window to do a redirect to the login page.

// The error handler for the ajax request.
            error: function (objXHR, status, errorThrown) {
...
                // Redirect to login if the ajax request returned a 401
                if (objXHR.status == "401") {
                    window.location.reload();
                    return;
                }
...
            }

If you can add an error handler to the Microsoft version of the ajax calls, you can probably do something similar.

womp
I'll give this a try, but moving everything to JQuery is not an option. I am a little green in JQ, can you point me on how to add a 'wrapper' around $.ajax (i.e. a link)? And has anyone done this in MVC/AJAX.BeginForm?
davewilliams459
Come to think of it, I wonder if the AJAX.BeginForm onsuccess JS function could handle this, but It would have to know if the return status was 401.
davewilliams459
A wrapper is just a function or a class that you would use throughout your app that calls the method in question. You can do setup and pre/post processing on the data that goes to/from the method.
womp
OIC, just a 'wrap' around the .ajax call, JQ error handling. I thought you were extending the .ajax method.
davewilliams459