Hi I am learning Ajax + MVC. I figured it would be nice for the Controller to automatically handle ajax-aware Redirect(). After some digging, I found the code from this link. The code below is totally transparent to user, a user can just call Redirect(someUrlString) without needing to worry about difference between normal/ajax calls. Makes it very neat and cool.
public abstract class BaseController : System.Web.Mvc.Controller {
//turn into ajax aware redirect
protected override RedirectResult Redirect(string url) {
return new AjaxAwareRedirectResult(url);
}
}
and ...
public class AjaxAwareRedirectResult : RedirectResult {
public AjaxAwareRedirectResult(string url) : base(url) { }
public override void ExecuteResult(ControllerContext context) {
if (context.RequestContext.HttpContext.Request.IsAjaxRequest()) {
string desturl = UrlHelper.GenerateContentUrl(Url, context.HttpContext);
JavaScriptResult result = new JavaScriptResult() {
Script = "window.location='" + desturl + "';" };
result.ExecuteResult(context);
}
else { base.ExecuteResult(context); }
}
}
However, it is not complete. Challenge is:
RedirectToRouteResult RedirectToAction
(ActionResult result)
is not there yet (Very handy especially for T4MVC).
As I am still new to MVC, I tried, but I am not knowledgeable enough to sufficiently figure out how to write this myself. Could any of you experts please help me with this? so I can learn it from your code? Thank you very much.