Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MVC extending controller to have ajax-aware Redirect functionality

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.

like image 846
Tom Avatar asked Jun 24 '12 06:06

Tom


1 Answers

Here is the quick simple solution I use for Ajax aware redirection in my project..

  1. Create a class AjaxRedirectAttribute for action.

        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 + "';"
                    };
                }
            }
        }
    
  2. User this attribute as below to either redirect to other page or to return some result from action.

    [AjaxRedirect]
    public ActionResult MyAction(FormCollection frmcol)
    {
        // some code here
        if (UserId != 0)
        {
            return Redirect(this.Url.Action("Action", "Controller"));
        }
        else
        {
            return Content("Error message here.");
        }
    }
    
like image 101
Kirtan Pandya Avatar answered Nov 05 '22 21:11

Kirtan Pandya