views:

46

answers:

2

I'm building an asp.net mvc2 website and using a lot of ajax form elementes (Ajax.BeginForm to be exact) to asynchronously populate data on the page. I would like to redirect the user to the sign in page after x amount of time of inactivity on the site. When I do this currently, either through ActionExecutingContext, or HttpContext, the sign in page is populated in the current div element for that ajax form, instead of the entire page. Any thoughts on how to get it to redirect the current page?

A: 

If you want to redirect the user to another page (login page) when the form is submitted then why use ajax ? AJAX is best used for the manipulation of a part of the page not and the whole page.

The Ajax.BeginForm has an option object to specify the id of the HTML element to be updated and the type of the update

example

Ajax.BeginForm("Create", "Project", 
                new AjaxOptions() { 
                                   UpdateTargetId = "projectform", 
                                   InsertionMode = InsertionMode.Replace, 
                                   HttpMethod = "Post" })

The InsertionMode option has three values Replace, InsertAfter, InsertBefore

In your case i don't know if you can target the HTML tag in the UpdateTargetId. but as i said earlier it's better to use a normal request in this scenario and not to update the whole page using Ajax.

about redirecting the user to the sign in page after a time of inactivity. I believe Sessions is the right way to do that. I'm not sure though how it's done.

Update :

i hope this link will help : http://blog.tallan.com/2010/06/25/handle-asp-net-mvc-session-expiration/

Manaf Abu.Rous
I'm doing my Ajax forms just as you show here, only difference is all of my calls are Get's, not Post's. That works just fine. I am also using Sessions to control the time out of the user, that also works fine. It's when the two of them work side by side, i get the issue. Here's what would happen based off of the example you just posted: 1. projectForm control updates as expected2. user walks away for a long time, comes back tries to post again3. session expires and wants to redirect them to signin.aspx.4. only the projectFrom element renders the signin.aspx html.
Justin Williams
Oh ,,, I get your problem now. maybe this will helphttp://stackoverflow.com/questions/2259225/how-can-i-redirect-in-asp-mvc-when-an-ajax-request-is-made(see the second solution)
Manaf Abu.Rous
Thanks man for the quick responses, your posts definitely got me on the right track, with a few modifications. I posted an answer for reference.
Justin Williams
+1  A: 

Solution:

override the OnActionExecuted event in your base controller, and create a RedirectResult to call into for wherever you want to redirect. Add the following code:

protected RedirectResult Redirect(string url, ActionExecutedContext filterContext)
{
    return new AjaxErrorRedirectResult(url, filterContext);
}

public class AjaxErrorRedirectResult : RedirectResult
    {
        public AjaxErrorRedirectResult(string url, ActionExecutedContext filterContext)
            : base(url)
        {
            ExecuteResult(filterContext.Controller.ControllerContext);
        }

        public override void ExecuteResult(ControllerContext context)
        {
            if (context.RequestContext.HttpContext.Request.IsAjaxRequest())
            {
                string redirectUrl = "www.google.com";

                JavaScriptResult result = new JavaScriptResult()
                {
                    Script = "self.parent.location='" + redirectUrl + "';"
                };
                result.ExecuteResult(context);
            }
            else
            {
                base.ExecuteResult(context);
            }
        }
    }
Justin Williams