using System; using Oqtane.Shared; namespace Oqtane.Models { /// /// A route is comprised of multiple components ( some optional depending on context ) /// {scheme}://{hostname}/{aliaspath}/{pagepath}/*/{moduleid}/{action}/!/{urlparameters}?{query}#{fragment} /// public class Route { /// /// parameterless constructor to prevent deserialization exceptions /// public Route() { } /// /// default constructor /// the route parameter can be obtained from NavigationManager.Uri on client or HttpContext.Request.GetEncodedUrl() on server /// the aliaspath parameter can be obtained from PageState.Alias.Path on client or TenantManager.GetAlias().Path on server /// public Route(string route, string aliaspath) { Uri uri = new Uri(route); Authority = uri.Authority; Scheme = uri.Scheme; Host = uri.Host; Port = uri.Port.ToString(); Query = uri.Query; Fragment = uri.Fragment; AbsolutePath = uri.AbsolutePath; PathAndQuery = uri.PathAndQuery; AliasPath = aliaspath; PagePath = AbsolutePath; ModuleId = "-1"; Action = Constants.DefaultAction; UrlParameters = ""; if (AliasPath.Length != 0 && PagePath.StartsWith("/" + AliasPath)) { PagePath = PagePath.Substring(AliasPath.Length + 1); } int pos = PagePath.IndexOf("/" + Constants.UrlParametersDelimiter + "/"); if (pos != -1) { if (pos + 3 < PagePath.Length) { UrlParameters = PagePath.Substring(pos + 3); } PagePath = PagePath.Substring(0, pos); } pos = PagePath.IndexOf("/" + Constants.ModuleDelimiter + "/"); if (pos != -1) { if (pos + 3 < PagePath.Length) { ModuleId = PagePath.Substring(pos + 3); } PagePath = PagePath.Substring(0, pos); } if (ModuleId.Length != 0) { pos = ModuleId.IndexOf("/"); if (pos != -1) { Action = ModuleId.Substring(pos + 1); Action = (!string.IsNullOrEmpty(Action)) ? Action : Constants.DefaultAction; ModuleId = ModuleId.Substring(0, pos); ModuleId = (int.TryParse(ModuleId, out _)) ? ModuleId : "-1"; } } if (PagePath.StartsWith("/")) { PagePath = (PagePath.Length == 1) ? "" : PagePath.Substring(1); } if (PagePath.EndsWith("/")) { PagePath = PagePath.Substring(0, PagePath.Length - 1); } } /// /// The host name or IP address and port number (does not include scheme or trailing slash) /// public string Authority { get; set; } /// /// A fully qualified route contains a scheme (ie. http, https ) /// public string Scheme { get; set; } /// /// A fully qualified route contains a host name. The host name may include a port number. /// public string Host { get; set; } /// /// A host name may contain a port number /// public string Port { get; set; } /// /// The absolute path for the route /// public string AbsolutePath { get; set; } /// /// The absolute path for the route including the querystring /// public string PathAndQuery { get; set; } /// /// An absolute path may contain an alias path /// public string AliasPath { get; set; } /// /// A absolute path may contain a page path. /// public string PagePath { get; set; } /// /// A route may contain a module id (ie. when created using EditUrl) located after the module delimiter segment (/*/). /// public string ModuleId { get; set; } /// /// A route may contain an action (ie. when created using EditUrl) located after the module id. /// public string Action { get; set; } /// /// A route may contain parameters located after the url parameter delimiter segment (/!/). /// public string UrlParameters { get; set; } /// /// All querystring parameters (prefixed with a ? delimiter) /// public string Query { get; set; } /// /// A route may contain a fragment located after the # delimiter /// public string Fragment { get; set; } /// /// The root url contains the resource identifier for the root of an Oqtane installation ( including scheme ) /// public string RootUrl { get { return Scheme + "://" + Authority; } } /// /// The site url contains the resource identifier for the home page of a specific Oqtane site ( including scheme and possibly an alias path ) /// public string SiteUrl { get { return Scheme + "://" + Authority + ((!string.IsNullOrEmpty(AliasPath)) ? "/" + AliasPath : ""); } } } }