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