public static void RedirectToMobileVersionIfNeeded(string mobileAddress, HttpContext context) { try { if (string.IsNullOrEmpty(mobileAddress) || !UrlCheckRegex.IsMatch(context.Request.GetUrlRewriter().ToString()) || !IsRequestMatchesMobile(context.Request.UserAgent, true) || CookiesManager.IsMobileBlocked() || CoreContext.Configuration.YourDocsDemo) { return; } //TODO: check user status to display desktop or mobile version if (mobileAddress.StartsWith("~")) { //Resolve to current mobileAddress = VirtualPathUtility.ToAbsolute(mobileAddress); } if (mobileAddress.Contains(TenantReplacePattern)) { var tennant = CoreContext.TenantManager.GetCurrentTenant(); mobileAddress = mobileAddress.Replace(TenantReplacePattern, tennant.TenantAlias); } var redirectUri = Uri.IsWellFormedUriString(mobileAddress, UriKind.Absolute) ? new Uri(mobileAddress) : new Uri(context.Request.GetUrlRewriter(), mobileAddress); if (redirectUri.Equals(context.Request.GetUrlRewriter())) { return; } var builder = new UriBuilder(redirectUri); var abspath = context.Request.GetUrlRewriter().AbsolutePath; if (!string.IsNullOrEmpty(abspath) && abspath.EndsWith("default.aspx", StringComparison.InvariantCultureIgnoreCase)) { abspath = abspath.Substring(0, abspath.Length - "default.aspx".Length); } builder.Path += abspath; builder.Query += context.Request.GetUrlRewriter().Query.TrimStart('?'); redirectUri = builder.Uri; LogManager.GetLogger("ASC.Mobile.Redirect").DebugFormat("Redirecting url:'{1}' to mobile. UA={0}", context.Request.UserAgent, context.Request.GetUrlRewriter()); context.Response.Redirect(redirectUri.ToString(), true); } catch (ThreadAbortException) { //Don't do nothing } catch (Exception e) { //If error happens it's not so bad as you may think. We won't redirect user to mobile version. LogManager.GetLogger("ASC.Mobile.Redirect").Error("failed to redirect user to mobile", e); } }
public static void RedirectToMobileVersionIfNeeded(string mobileAddress, HttpContext context) { try { if (!string.IsNullOrEmpty(mobileAddress) && UrlCheckRegex.IsMatch(context.Request.GetUrlRewriter().ToString()) && IsUserAgentMatchesMobile(context.Request.UserAgent, RedirectRegex) && !CookiesManager.IsMobileBlocked()) { //TODO: check user status to display desktop or mobile version if (mobileAddress.StartsWith("~")) { //Resolve to current mobileAddress = VirtualPathUtility.ToAbsolute(mobileAddress); } if (mobileAddress.Contains(TenantReplacePattern)) { var tennant = CoreContext.TenantManager.GetCurrentTenant(); mobileAddress = mobileAddress.Replace(TenantReplacePattern, tennant.TenantAlias); } var redirectUri = Uri.IsWellFormedUriString(mobileAddress, UriKind.Absolute) ? new Uri(mobileAddress) : new Uri(context.Request.GetUrlRewriter(), mobileAddress); if (!redirectUri.Equals(context.Request.GetUrlRewriter())) { UriBuilder builder = new UriBuilder(redirectUri); builder.Path += context.Request.GetUrlRewriter().AbsolutePath; builder.Query += context.Request.GetUrlRewriter().Query.TrimStart('?'); redirectUri = builder.Uri; LogManager.GetLogger("ASC.Mobile.Redirect").DebugFormat("Redirecting url:'{1}' to mobile. UA={0}", context.Request.UserAgent, context.Request.GetUrlRewriter()); context.Response.Redirect(redirectUri.ToString(), true); } } } catch (ThreadAbortException) { //Don't do nothing } catch (Exception e) { //If error happens it's not so bad as you may think. We won't redirect user to mobile version. LogManager.GetLogger("ASC.Mobile.Redirect").Error("failed to redirect user to mobile", e); } }