Home > ASP.NET MVC > ASP.NET MVC Ajax Redirect

ASP.NET MVC Ajax Redirect


I’ve had several occasions where I have needed to make an Ajax request to a secure action on the server (i.e. an action that requires the user to be logged in). The problem is…sometimes the user’s session will timeout between the time they access the secure page and the time they make the secure Ajax request. My action notices this and redirects the user to the login page…but if its an Ajax request then a simple 302 redirect just won’t work. Here’s a method that will.

public class MyBaseController : System.Web.Mvc.Controller
{
    protected override RedirectResult Redirect(string url)
    {
        return new AjaxAwareRedirectResult(url);
    }
}

public class AjaxAwareRedirectResult : RedirectResult
{
    public AjaxAwareRedirectResult(string url)
        : base(url)
    {
    }

    public override void ExecuteResult(ControllerContext context)
    {
        if (context.RequestContext.HttpContext.Request.IsAjaxRequest())
        {
            string destinationUrl = UrlHelper.GenerateContentUrl(Url, context.HttpContext);

            JavaScriptResult result = new JavaScriptResult()
            {
                Script = "window.location='" + destinationUrl + "';"
            };
            result.ExecuteResult(context);
        }
        else
            base.ExecuteResult(context);
    }
}

Now you can use the controller’s Redirect() function as usual…and it will automatically detect if it needs to perform a 302 redirect or an Ajax redirect.

Cheers!

kick it on DotNetKicks.com

About these ads
Categories: ASP.NET MVC
  1. July 27, 2010 at 1:20 pm

    Neat stuff dude! Thanks!
    I’ll try to extend it a bit to support the other redirect methods such as RedirectToAction and let you know if I succeed!

    • July 27, 2010 at 2:29 pm

      Awesome. I’d be interested to hear about your findings.

  2. Evan
    August 18, 2010 at 7:38 pm

    Brilliant! I modified it to work with RedirectResult also (so that it’s compatible with T4MVC). Many, many thanks.

  3. Jon Jenkins
    September 25, 2010 at 9:37 pm

    Great stuff my friend. Thanks for providing something simple that does the job and saves me the cycle.

  4. Feng Chun
    October 1, 2010 at 10:16 am

    it seems that i need to change the script to:
    “window.location='” + destinationUrl + “‘;”
    to make it work for me

  5. Feng Chun
    October 1, 2010 at 10:17 am

    sorry, it seems removed the script tag.

    I mean I need to add the script tag around the line to make it works

  6. jerome
    December 15, 2010 at 12:03 am

    good example, keep up the good work :-)

  7. January 19, 2011 at 2:43 pm

    Hey nice post. I wanted to use your code but couldn’t modify the RedirectResult class, mostly because I’m using the strongly typed redirect helpers from MvcContrib and because I didn’t want to worry about always substituting a base class. So I went about it another way. Just create a new type of ActionResult attribute then just tag your controllers with it:

    public class AjaxRedirectAttribute : ActionFilterAttribute
    {
    public override void OnActionExecuted(ActionExecutedContext filterContext)
    {
    var result = filterContext.Result as RedirectResult;
    if (result != null && filterContext.HttpContext.Request.IsAjaxRequest())
    {
    string destinationUrl = UrlHelper.GenerateContentUrl(result.Url, filterContext.HttpContext);

    filterContext.Result = new JavaScriptResult()
    {
    Script = “window.location='” + destinationUrl + “‘;”
    };
    }
    }
    }

    Put [AjaxRedirect] over your controller. Then, you don’t have to worry about subclassing RedirectResult. It’ll just work!

  8. Durga Prasad
    May 6, 2011 at 6:15 am

    It is really cool…. it is working… :)

    Thank you.

  9. July 18, 2011 at 7:49 am

    Soory guyz how to apply this please help me

  10. Rudi
    September 1, 2011 at 1:19 am

    Awsome Mate, thanks allot that made my day.

  11. meysam
    September 29, 2011 at 2:13 am

    sorry.
    this is my correct controller:

    Function Create(ByVal tbl As Table1) As ActionResult
    db.AddToTable1(tbl)
    db.SaveChanges()
    Return RedirectToAction(“Index”)
    End Function

  12. Lalit
    October 17, 2012 at 2:36 am

    Beautiful…Beautifull…absolutely beautiful. Its called “Art”

  13. January 18, 2013 at 7:17 am

    It is great work but i understand it working theoretically but does not implementing in practically so kindly give me some your important suggestion how it work with mvc3.

  1. July 5, 2010 at 10:28 am
  2. January 7, 2011 at 4:16 pm
  3. May 21, 2012 at 4:58 am
  4. August 16, 2012 at 2:55 pm
  5. December 10, 2012 at 6:53 am

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.

%d bloggers like this: