Hi, I am learning Ajax + MVC. I figured it would be nice if the controller automatically handles ajax-aware Redirect (). After some digging, I found the code from this one. The code below is completely transparent to the user, the user can simply call Redirect (someUrlString) without worrying about the difference between normal / ajax calls. Makes it very neat and cool.
public abstract class BaseController : System.Web.Mvc.Controller {
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. Task:
RedirectToRouteResult (result of ActionResult) RedirectToAction
not yet (very convenient for T4MVC).
MVC, , , , . - , , ? ? .