From 9822ad429ef9d1975546e486eecd1d77e1398798 Mon Sep 17 00:00:00 2001 From: Uncled1023 Date: Tue, 22 Dec 2015 23:29:02 -0800 Subject: [PATCH] Modifying url routing --- Teknik/Global.asax.cs | 7 ---- Teknik/Helpers/UrlExtensions.cs | 63 +++++++++++++++++++++++++++------ 2 files changed, 52 insertions(+), 18 deletions(-) diff --git a/Teknik/Global.asax.cs b/Teknik/Global.asax.cs index 2de8d59..7f8418e 100644 --- a/Teknik/Global.asax.cs +++ b/Teknik/Global.asax.cs @@ -30,13 +30,6 @@ namespace Teknik BundleConfig.RegisterBundles(BundleTable.Bundles); } - protected void Application_EndRequest(object sender, EventArgs e) - { - var context = new HttpContextWrapper(Context); - context.Response.SuppressFormsAuthenticationRedirect = true; - context.Response.TrySkipIisCustomErrors = true; - } - protected void Application_PostAuthenticateRequest(Object sender, EventArgs e) { if (FormsAuthentication.CookiesSupported == true) diff --git a/Teknik/Helpers/UrlExtensions.cs b/Teknik/Helpers/UrlExtensions.cs index 373fe5c..9c7ba9c 100644 --- a/Teknik/Helpers/UrlExtensions.cs +++ b/Teknik/Helpers/UrlExtensions.cs @@ -1,8 +1,10 @@ using System; using System.Collections.Generic; +using System.Collections.Specialized; using System.Linq; using System.Web; using System.Web.Mvc; +using System.Web.UI; namespace Teknik { @@ -22,8 +24,7 @@ namespace Teknik { Uri requestUrl = url.RequestContext.HttpContext.Request.Url; string host = url.RequestContext.HttpContext.Request.Url.Authority; - - string firstSub = string.Empty; + string paramSub = string.Empty; string domain = host; string rightUrl = string.Empty; @@ -41,33 +42,73 @@ namespace Teknik // Grab the sub from parameters if it exists string subParam = url.RequestContext.HttpContext.Request.QueryString["sub"]; // A subdomain specified as a query parameter takes precedence over the hostname. - + string fullHost = url.RequestContext.HttpContext.Request.Headers["Host"]; + // If the param is not being used, we will use the curSub if (string.IsNullOrEmpty(subParam)) { // If we are in dev, we need to keep it in dev - firstSub = (curSub == "dev") ? "dev" : sub; - rightUrl = url.Action(action, controller, routeValues); + string firstSub = (curSub == "dev") ? "dev" : sub; + rightUrl = url.Action(action, controller, Utility.Merge(new { sub = sub }, routeValues)); + + domain = (string.IsNullOrEmpty(firstSub)) ? domain : firstSub + "." + domain; } else { - // sub within param will always be on the dev subdomain - firstSub = (string.IsNullOrEmpty(curSub)) ? string.Empty : "dev"; + // if (subParam != "dev") { - rightUrl = url.Action(action, controller, Utility.Merge(new { sub = sub }, routeValues)); + // replace the host and sub param in the context in order to generate the correct URLs + string newUrl = url.RequestContext.HttpContext.Request.Url.AbsoluteUri.SetUrlParameter("sub", sub); + url.RequestContext.HttpContext.RewritePath(url.RequestContext.HttpContext.Request.Path, url.RequestContext.HttpContext.Request.PathInfo, newUrl.GetUrlParameters()); + // get the url for the new sub + rightUrl = url.Action(action, controller, routeValues); + var page = url.RequestContext.HttpContext.Handler as Page; + rightUrl = page.GetRouteUrl(new { sub = sub }); + // Reset the url + string oldUrl = url.RequestContext.HttpContext.Request.Url.AbsoluteUri.SetUrlParameter("sub", subParam); + url.RequestContext.HttpContext.RewritePath(url.RequestContext.HttpContext.Request.Path, url.RequestContext.HttpContext.Request.PathInfo, newUrl.GetUrlParameters()); } - else + else // 'dev' is in the param, so we need to generate the action based on { rightUrl = url.Action(action, controller, routeValues); } + + // if using sub param, keep domain as is + domain = host; } - domain = (string.IsNullOrEmpty(firstSub)) ? domain : firstSub + "." + domain; - string absoluteAction = string.Format("{0}://{1}{2}", url.RequestContext.HttpContext.Request.Url.Scheme, domain, rightUrl); + + if (!string.IsNullOrEmpty(subParam) && subParam != "dev") + { + absoluteAction = absoluteAction.SetUrlParameter("sub", sub); + } return absoluteAction; } + public static string GetUrlParameters(this string url) + { + Uri uri = new Uri(url); + var queryParts = HttpUtility.ParseQueryString(uri.Query); + return queryParts.ToString(); + } + + public static string SetUrlParameter(this string url, string paramName, string value) + { + return new Uri(url).SetParameter(paramName, value).ToString(); + } + + public static Uri SetParameter(this Uri url, string paramName, string value) + { + var queryParts = HttpUtility.ParseQueryString(url.Query); + queryParts[paramName] = value; + return new Uri(url.AbsoluteUriExcludingQuery() + '?' + queryParts.ToString()); + } + + public static string AbsoluteUriExcludingQuery(this Uri url) + { + return url.AbsoluteUri.Split('?').FirstOrDefault() ?? String.Empty; + } } } \ No newline at end of file