Created
March 7, 2012 06:14
-
-
Save sheastrickland/1991394 to your computer and use it in GitHub Desktop.
Simple server-side solution to jQuery Mobile + Ajax request + Redirect being transparently followed and not updating the data-url / page
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<div id="container" data-role="page" @(Html.DataUrl(Request, TempData))> | |
@RenderSection("Header", false) | |
@RenderBody() | |
@RenderSection("Footer", false) | |
</div> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public interface IRedirectProcessor | |
{ | |
string Process(ResultExecutedContext context); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class MobileAjaxRedirectDataUrlFilter : IResultFilter | |
{ | |
public const string DataUrlKey = "DataUrl"; | |
readonly IEnumerable<IRedirectProcessor> _processors; | |
public MobileAjaxRedirectDataUrlFilter(IEnumerable<IRedirectProcessor> processors) | |
{ | |
_processors = processors; | |
} | |
public void OnResultExecuting(ResultExecutingContext filterContext) | |
{ | |
} | |
public void OnResultExecuted(ResultExecutedContext filterContext) | |
{ | |
if (!filterContext.HttpContext.Request.IsAjaxRequest() || !filterContext.HttpContext.Request.IsMobileView()) | |
return; | |
foreach (var redirect in _processors.Select(processor => processor.Process(filterContext)).Where(redirect => redirect != null)) | |
{ | |
filterContext.Controller.TempData[DataUrlKey] = redirect; | |
break; | |
} | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public static class MobileHtmlExtensions | |
{ | |
public static IHtmlString DataUrl(this HtmlHelper html, HttpRequestBase request, TempDataDictionary tempData) | |
{ | |
if (request.IsAjaxRequest() && request.IsMobileView() && tempData.ContainsKey(Filters.MobileAjaxRedirectDataUrlFilter.DataUrlKey)) | |
{ | |
return html.Raw(string.Format("data-url=\"{0}\"", tempData[Filters.MobileAjaxRedirectDataUrlFilter.DataUrlKey])); | |
} | |
return MvcHtmlString.Empty; | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class RedirectResultProcessor : IRedirectProcessor | |
{ | |
public string Process(ResultExecutedContext context) | |
{ | |
var redirectResult = context.Result as RedirectResult; | |
return redirectResult == null | |
? null | |
: UrlHelper.GenerateContentUrl(redirectResult.Url, context.HttpContext); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class RedirectToRouteResultProcessor : IRedirectProcessor | |
{ | |
public string Process(ResultExecutedContext context) | |
{ | |
var redirectToRouteResult = context.Result as RedirectToRouteResult; | |
return redirectToRouteResult == null | |
? null | |
: UrlHelper.GenerateUrl(redirectToRouteResult.RouteName, null /* actionName */, null /* controllerName */, redirectToRouteResult.RouteValues, RouteTable.Routes, context.RequestContext, false /* includeImplicitMvcValues */); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment