optimize ParseParameters to use native Uri class
This commit is contained in:
parent
76680777ff
commit
dd1d0d1cb8
|
@ -384,9 +384,7 @@
|
||||||
_path = _name;
|
_path = _name;
|
||||||
}
|
}
|
||||||
|
|
||||||
var fragment = (_path.Contains("?")) ? _path.Substring(_path.IndexOf("?")) : "";
|
(_path, string parameters) = Utilities.ParsePath(_path);
|
||||||
fragment = (_path.Contains("#")) ? _path.Substring(_path.IndexOf("#")) : "";
|
|
||||||
_path = (!string.IsNullOrEmpty(fragment)) ? _path.Replace(fragment, "") : _path;
|
|
||||||
|
|
||||||
if (_path.Contains("/"))
|
if (_path.Contains("/"))
|
||||||
{
|
{
|
||||||
|
@ -412,7 +410,7 @@
|
||||||
page.Path = parent.Path + "/" + Utilities.GetFriendlyUrl(_path);
|
page.Path = parent.Path + "/" + Utilities.GetFriendlyUrl(_path);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
page.Path += fragment;
|
page.Path += parameters;
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
|
|
|
@ -526,9 +526,7 @@
|
||||||
_path = _name;
|
_path = _name;
|
||||||
}
|
}
|
||||||
|
|
||||||
var fragment = (_path.Contains("?")) ? _path.Substring(_path.IndexOf("?")) : "";
|
(_path, string parameters) = Utilities.ParsePath(_path);
|
||||||
fragment = (_path.Contains("#")) ? _path.Substring(_path.IndexOf("#")) : "";
|
|
||||||
_path = (!string.IsNullOrEmpty(fragment)) ? _path.Replace(fragment, "") : _path;
|
|
||||||
|
|
||||||
if (_path.Contains("/"))
|
if (_path.Contains("/"))
|
||||||
{
|
{
|
||||||
|
@ -554,7 +552,7 @@
|
||||||
_page.Path = parent.Path + "/" + Utilities.GetFriendlyUrl(_path);
|
_page.Path = parent.Path + "/" + Utilities.GetFriendlyUrl(_path);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
_page.Path += fragment;
|
_page.Path += parameters;
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
|
|
|
@ -20,69 +20,60 @@ namespace Oqtane.Shared
|
||||||
return $"{type.Namespace}, {assemblyName}";
|
return $"{type.Namespace}, {assemblyName}";
|
||||||
}
|
}
|
||||||
|
|
||||||
public static (string UrlParameters, string Querystring, string Anchor) ParseParameters(string parameters)
|
public static (string UrlParameters, string Querystring, string Fragment) ParseParameters(string url)
|
||||||
{
|
{
|
||||||
|
// /path/urlparameters
|
||||||
// /urlparameters /urlparameters?Id=1 /urlparameters#5 /urlparameters?Id=1#5 /urlparameters?reload#5
|
// /urlparameters /urlparameters?Id=1 /urlparameters#5 /urlparameters?Id=1#5 /urlparameters?reload#5
|
||||||
|
|
||||||
// Id=1 Id=1#5 reload#5 reload
|
// Id=1 Id=1#5 reload#5 reload
|
||||||
|
|
||||||
// #5
|
// #5
|
||||||
|
|
||||||
var urlparameters = string.Empty;
|
if (!url.StartsWith("/")) // paths always start with "/"
|
||||||
var querystring = string.Empty;
|
|
||||||
var anchor = string.Empty;
|
|
||||||
|
|
||||||
if (parameters.Contains('#'))
|
|
||||||
{
|
{
|
||||||
anchor = parameters.Split('#').Last();
|
url = ((!url.StartsWith("#")) ? "/?" : "/") + url;
|
||||||
parameters = parameters.Replace("#" + anchor, "");
|
}
|
||||||
|
url = ((!url.Contains("://")) ? Constants.PackageRegistryUrl : "") + url;
|
||||||
|
|
||||||
|
var uri = new Uri(url);
|
||||||
|
var querystring = uri.Query.Replace("?", "");
|
||||||
|
var fragment = uri.Fragment.Replace("#", "");
|
||||||
|
var urlparameters = uri.LocalPath;
|
||||||
|
urlparameters = (urlparameters == "/") ? "" : urlparameters;
|
||||||
|
|
||||||
|
if (urlparameters.Contains(Constants.UrlParametersDelimiter))
|
||||||
|
{
|
||||||
|
urlparameters = urlparameters.Substring(urlparameters.IndexOf(Constants.UrlParametersDelimiter) + 1);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (parameters.Contains('?'))
|
return (urlparameters, querystring, fragment);
|
||||||
{
|
|
||||||
urlparameters = parameters.Split('?').First();
|
|
||||||
querystring = parameters.Replace(urlparameters + "?", "");
|
|
||||||
}
|
|
||||||
else if (parameters.Contains('/'))
|
|
||||||
{
|
|
||||||
urlparameters = parameters;
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
querystring = parameters;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return (urlparameters, querystring, anchor);
|
public static (string Path, string Parameters) ParsePath(string url)
|
||||||
}
|
{
|
||||||
|
url = (!url.StartsWith("/") ? "/" : "") + url;
|
||||||
|
|
||||||
private static (string Path, string Parameters) ParsePath(string path, string parameters)
|
(string path, string querystring, string fragment) = ParseParameters(url);
|
||||||
|
|
||||||
|
var uriBuilder = new UriBuilder
|
||||||
{
|
{
|
||||||
if (!string.IsNullOrEmpty(path))
|
Path = path,
|
||||||
{
|
Query = querystring,
|
||||||
if (path.Contains('?'))
|
Fragment = fragment
|
||||||
{
|
};
|
||||||
parameters = "?" + path.Split('?').Last();
|
|
||||||
path = path.Split('?').First();
|
return (uriBuilder.Path, uriBuilder.Uri.Query + uriBuilder.Uri.Fragment);
|
||||||
}
|
|
||||||
if (path.Contains('#'))
|
|
||||||
{
|
|
||||||
parameters = "#" + path.Split('#').Last();
|
|
||||||
path = path.Split('#').First();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return (path, parameters);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public static string NavigateUrl(string alias, string path, string parameters)
|
public static string NavigateUrl(string alias, string path, string parameters)
|
||||||
{
|
{
|
||||||
string urlparameters;
|
if (!string.IsNullOrEmpty(parameters))
|
||||||
string querystring;
|
{
|
||||||
string anchor;
|
// parse path
|
||||||
|
(path, _) = ParsePath(path);
|
||||||
|
|
||||||
// parse path (in case parameters are embedded in path)
|
|
||||||
(path, parameters) = ParsePath(path, parameters);
|
|
||||||
// parse parameters
|
// parse parameters
|
||||||
(urlparameters, querystring, anchor) = ParseParameters(parameters);
|
(string urlparameters, string querystring, string fragment) = ParseParameters(parameters);
|
||||||
|
|
||||||
|
// add urlparameters to path
|
||||||
if (!string.IsNullOrEmpty(urlparameters))
|
if (!string.IsNullOrEmpty(urlparameters))
|
||||||
{
|
{
|
||||||
if (urlparameters.StartsWith("/")) urlparameters = urlparameters.Remove(0, 1);
|
if (urlparameters.StartsWith("/")) urlparameters = urlparameters.Remove(0, 1);
|
||||||
|
@ -93,15 +84,19 @@ namespace Oqtane.Shared
|
||||||
var uriBuilder = new UriBuilder
|
var uriBuilder = new UriBuilder
|
||||||
{
|
{
|
||||||
Path = !string.IsNullOrEmpty(alias)
|
Path = !string.IsNullOrEmpty(alias)
|
||||||
? (!string.IsNullOrEmpty(path))
|
? (!string.IsNullOrEmpty(path)) ? $"{alias}{path}": $"{alias}"
|
||||||
? $"{alias}/{path}"
|
|
||||||
: $"{alias}"
|
|
||||||
: $"{path}",
|
: $"{path}",
|
||||||
Query = querystring,
|
Query = querystring,
|
||||||
|
Fragment = fragment
|
||||||
};
|
};
|
||||||
|
path = uriBuilder.Uri.PathAndQuery;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
path = ((!string.IsNullOrEmpty(alias)) ? alias + "/" : "") + path;
|
||||||
|
}
|
||||||
|
|
||||||
anchor = string.IsNullOrEmpty(anchor) ? "" : "#" + anchor;
|
return path;
|
||||||
return uriBuilder.Uri.PathAndQuery + anchor;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public static string EditUrl(string alias, string path, int moduleid, string action, string parameters)
|
public static string EditUrl(string alias, string path, int moduleid, string action, string parameters)
|
||||||
|
|
Loading…
Reference in New Issue
Block a user