From time to time, we need to selectively redirect the browser to another location as a
result of an AJAX action. Just returning RedirectResult won’t do the trick (even if we
are used to similar functionality in ASP.NET AJAX in combination with WebForms ). Here
is a very simple RedirectResult thet will be appropriate in those scenarios:
public class AjaxRedirectResult : RedirectResult
{
public AjaxRedirectResult(string url): base(url){}
public override void ExecuteResult(ControllerContext context)
{
if (context.RequestContext.HttpContext.Request.IsAjaxRequest())
{
JavaScriptResult result = new JavaScriptResult()
{
Script = String.Format("window.location='{0}';",
UrlHelper.GenerateContentUrl(Url, context.HttpContext))
};
result.ExecuteResult(context);
}
else
base.ExecuteResult(context);
}
}
bcfbbf47-3cb6-4952-a56e-99f4b952cb15|38|5.0