Browse Source

Modifying url routing

tags/2.0.3
Teknikode 5 years ago
parent
commit
9822ad429e
2 changed files with 52 additions and 18 deletions
  1. 0
    7
      Teknik/Global.asax.cs
  2. 52
    11
      Teknik/Helpers/UrlExtensions.cs

+ 0
- 7
Teknik/Global.asax.cs View File

@@ -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)

+ 52
- 11
Teknik/Helpers/UrlExtensions.cs View File

@@ -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);
}
}

domain = (string.IsNullOrEmpty(firstSub)) ? domain : firstSub + "." + domain;
// if using sub param, keep domain as is
domain = host;
}

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;
}
}
}

Loading…
Cancel
Save