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!
[…] to Vote[FriendFeed] ASP.NET MVC Ajax Redirect « Crafty Code (7/5/2010)Monday, July 05, 2010 from […]
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!
Awesome. I’d be interested to hear about your findings.
Brilliant! I modified it to work with RedirectResult also (so that it’s compatible with T4MVC). Many, many thanks.
Great stuff my friend. Thanks for providing something simple that does the job and saves me the cycle.
it seems that i need to change the script to:
“window.location='” + destinationUrl + “‘;”
to make it work for me
sorry, it seems removed the script tag.
I mean I need to add the script tag around the line to make it works
good example, keep up the good work 🙂
[…] referred to in other posts on SO, which outline steps similar to the ones in this blog post: http://craftycodeblog.com/2010/05/15/asp-net-mvc-ajax-redirect/However, perhaps because I’m using authorize annotations such as:Authorize(Roles = "Patient, […]
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!
This helped my situation more effectively as my page wasn’t relaoding after the redirect when using Ajax.Begin form.
It is really cool…. it is working… 🙂
Thank you.
Soory guyz how to apply this please help me
Awsome Mate, thanks allot that made my day.
sorry.
this is my correct controller:
Function Create(ByVal tbl As Table1) As ActionResult
db.AddToTable1(tbl)
db.SaveChanges()
Return RedirectToAction(“Index”)
End Function
[…] When a session times out, upon posting to the application, mvc2 returns a redirect, which prompts the browser to retrieve my login page, which it then embeds within the current page. This produced one view embedded within my current view, which is obviously undesirable. I attempted to implement the solution referred to in other posts on SO, which outline steps similar to the ones in this blog post: http://craftycodeblog.com/2010/05/15/asp-net-mvc-ajax-redirect/ […]
[…] Solution: http://craftycodeblog.com/2010/05/15/asp-net-mvc-ajax-redirect/ […]
Beautiful…Beautifull…absolutely beautiful. Its called “Art”
[…] to these posts elsewhere on the web for helping out: http://craftycodeblog.com/2010/05/15/asp-net-mvc-ajax-redirect/ […]
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.
Elegant and clean. Thank you for this!