Merge pull request #2372 from sbwalker/dev
optimize Url Parameters and implement in Event Log
This commit is contained in:
commit
1f584d57ac
|
@ -130,13 +130,14 @@
|
||||||
private string _properties = string.Empty;
|
private string _properties = string.Empty;
|
||||||
private string _server = string.Empty;
|
private string _server = string.Empty;
|
||||||
|
|
||||||
|
public override string RouteTemplate => "/{id}";
|
||||||
public override SecurityAccessLevel SecurityAccessLevel => SecurityAccessLevel.Host;
|
public override SecurityAccessLevel SecurityAccessLevel => SecurityAccessLevel.Host;
|
||||||
|
|
||||||
protected override async Task OnInitializedAsync()
|
protected override async Task OnInitializedAsync()
|
||||||
{
|
{
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
_logId = Int32.Parse(PageState.QueryString["id"]);
|
_logId = Int32.Parse(UrlParameters["id"]);
|
||||||
var log = await LogService.GetLogAsync(_logId);
|
var log = await LogService.GetLogAsync(_logId);
|
||||||
if (log != null)
|
if (log != null)
|
||||||
{
|
{
|
||||||
|
|
|
@ -63,7 +63,7 @@ else
|
||||||
<th>@Localizer["Function"]</th>
|
<th>@Localizer["Function"]</th>
|
||||||
</Header>
|
</Header>
|
||||||
<Row>
|
<Row>
|
||||||
<td class="@GetClass(context.Function)"><ActionLink Action="Detail" Parameters="@($"id={context.LogId}")" ReturnUrl="@(NavigateUrl(PageState.Page.Path, $"level={_level}&function={_function}&rows={_rows}&page={_page}"))" ResourceKey="LogDetails" /></td>
|
<td class="@GetClass(context.Function)"><ActionLink Action="Detail" Parameters="@($"/{context.LogId}")" ReturnUrl="@(NavigateUrl(PageState.Page.Path, AddUrlParameters(_level, _function, _rows, _page)))" ResourceKey="LogDetails" /></td>
|
||||||
<td class="@GetClass(context.Function)">@context.LogDate</td>
|
<td class="@GetClass(context.Function)">@context.LogDate</td>
|
||||||
<td class="@GetClass(context.Function)">@context.Level</td>
|
<td class="@GetClass(context.Function)">@context.Level</td>
|
||||||
<td class="@GetClass(context.Function)">@context.Feature</td>
|
<td class="@GetClass(context.Function)">@context.Feature</td>
|
||||||
|
@ -99,29 +99,32 @@ else
|
||||||
private List<Log> _logs;
|
private List<Log> _logs;
|
||||||
private string _retention = "";
|
private string _retention = "";
|
||||||
|
|
||||||
|
public override string RouteTemplate => "/{level}/{function}/{rows}/{page}";
|
||||||
public override SecurityAccessLevel SecurityAccessLevel => SecurityAccessLevel.Host;
|
public override SecurityAccessLevel SecurityAccessLevel => SecurityAccessLevel.Host;
|
||||||
|
|
||||||
protected override async Task OnInitializedAsync()
|
protected override async Task OnInitializedAsync()
|
||||||
{
|
{
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
|
// external link to log item will display Details component
|
||||||
if (PageState.QueryString.ContainsKey("id") && int.TryParse(PageState.QueryString["id"], out int id))
|
if (PageState.QueryString.ContainsKey("id") && int.TryParse(PageState.QueryString["id"], out int id))
|
||||||
{
|
{
|
||||||
NavigationManager.NavigateTo(EditUrl(PageState.Page.Path, ModuleState.ModuleId, "Detail", $"id={id}"));
|
NavigationManager.NavigateTo(EditUrl(PageState.Page.Path, ModuleState.ModuleId, "Detail", $"id={id}"));
|
||||||
}
|
}
|
||||||
if (PageState.QueryString.ContainsKey("level"))
|
|
||||||
|
if (UrlParameters.ContainsKey("level"))
|
||||||
{
|
{
|
||||||
_level = PageState.QueryString["level"];
|
_level = UrlParameters["level"];
|
||||||
}
|
}
|
||||||
if (PageState.QueryString.ContainsKey("function"))
|
if (UrlParameters.ContainsKey("function"))
|
||||||
{
|
{
|
||||||
_function = PageState.QueryString["function"];
|
_function = UrlParameters["function"];
|
||||||
}
|
}
|
||||||
if (PageState.QueryString.ContainsKey("rows"))
|
if (UrlParameters.ContainsKey("rows"))
|
||||||
{
|
{
|
||||||
_rows = PageState.QueryString["rows"];
|
_rows = UrlParameters["rows"];
|
||||||
}
|
}
|
||||||
if (PageState.QueryString.ContainsKey("page") && int.TryParse(PageState.QueryString["page"], out int page))
|
if (UrlParameters.ContainsKey("page") && int.TryParse(UrlParameters["page"], out int page))
|
||||||
{
|
{
|
||||||
_page = page;
|
_page = page;
|
||||||
}
|
}
|
||||||
|
|
|
@ -121,11 +121,11 @@
|
||||||
|
|
||||||
_permissions = (string.IsNullOrEmpty(Permissions)) ? ModuleState.Permissions : Permissions;
|
_permissions = (string.IsNullOrEmpty(Permissions)) ? ModuleState.Permissions : Permissions;
|
||||||
_text = Localize(nameof(Text), _text);
|
_text = Localize(nameof(Text), _text);
|
||||||
|
_url = (ModuleId == -1) ? EditUrl(Action, _parameters) : EditUrl(ModuleId, Action, _parameters);
|
||||||
if (!string.IsNullOrEmpty(ReturnUrl))
|
if (!string.IsNullOrEmpty(ReturnUrl))
|
||||||
{
|
{
|
||||||
_parameters += ((!string.IsNullOrEmpty(_parameters)) ? "&" : "") + $"returnurl={WebUtility.UrlEncode(ReturnUrl)}";
|
_url += ((_url.Contains("?")) ? "&" : "?") + $"returnurl={WebUtility.UrlEncode(ReturnUrl)}";
|
||||||
}
|
}
|
||||||
_url = (ModuleId == -1) ? EditUrl(Action, _parameters) : EditUrl(ModuleId, Action, _parameters);
|
|
||||||
_authorized = IsAuthorized();
|
_authorized = IsAuthorized();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -15,6 +15,7 @@ namespace Oqtane.Modules
|
||||||
public abstract class ModuleBase : ComponentBase, IModuleControl
|
public abstract class ModuleBase : ComponentBase, IModuleControl
|
||||||
{
|
{
|
||||||
private Logger _logger;
|
private Logger _logger;
|
||||||
|
private Dictionary<string, string> _urlparameters;
|
||||||
|
|
||||||
protected Logger logger => _logger ?? (_logger = new Logger(this));
|
protected Logger logger => _logger ?? (_logger = new Logger(this));
|
||||||
|
|
||||||
|
@ -47,6 +48,20 @@ namespace Oqtane.Modules
|
||||||
|
|
||||||
public virtual List<Resource> Resources { get; set; }
|
public virtual List<Resource> Resources { get; set; }
|
||||||
|
|
||||||
|
// url parameters
|
||||||
|
public virtual string RouteTemplate { get; set; }
|
||||||
|
|
||||||
|
public Dictionary<string, string> UrlParameters {
|
||||||
|
get
|
||||||
|
{
|
||||||
|
if (_urlparameters == null)
|
||||||
|
{
|
||||||
|
_urlparameters = GetUrlParameters(RouteTemplate);
|
||||||
|
}
|
||||||
|
return _urlparameters;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// base lifecycle method for handling JSInterop script registration
|
// base lifecycle method for handling JSInterop script registration
|
||||||
|
|
||||||
protected override async Task OnAfterRenderAsync(bool firstRender)
|
protected override async Task OnAfterRenderAsync(bool firstRender)
|
||||||
|
@ -153,31 +168,26 @@ namespace Oqtane.Modules
|
||||||
return Utilities.ImageUrl(PageState.Alias, fileid, width, height, mode, position, background, rotate, recreate);
|
return Utilities.ImageUrl(PageState.Alias, fileid, width, height, mode, position, background, rotate, recreate);
|
||||||
}
|
}
|
||||||
|
|
||||||
public string AddUrlParameters(params string[] parameters)
|
public string AddUrlParameters(params object[] parameters)
|
||||||
{
|
{
|
||||||
return AddUrlParameters(PageState.Page.Path, parameters);
|
var url = "";
|
||||||
}
|
|
||||||
|
|
||||||
public string AddUrlParameters(string path, params string[] parameters)
|
|
||||||
{
|
|
||||||
var url = path + "/" + Constants.UrlParametersDelimiter;
|
|
||||||
for (var i = 0; i < parameters.Length; i++)
|
for (var i = 0; i < parameters.Length; i++)
|
||||||
{
|
{
|
||||||
url += "/" + parameters[i];
|
url += "/" + parameters[i].ToString();
|
||||||
}
|
}
|
||||||
return url;
|
return url;
|
||||||
}
|
}
|
||||||
|
|
||||||
// parameters template is in the form of a standard route template ie. "{id}/{name}"
|
// template is in the form of a standard route template ie. "/{id}/{name}" and produces dictionary of key/value pairs
|
||||||
public virtual Dictionary<string, string> GetUrlParameters(string parametersTemplate = "")
|
// if url parameters belong to a specific module you should embed a unique key into the route (ie. /!/blog/1) and validate the url parameter key in the module
|
||||||
|
public virtual Dictionary<string, string> GetUrlParameters(string template = "")
|
||||||
{
|
{
|
||||||
var urlParameters = new Dictionary<string, string>();
|
var urlParameters = new Dictionary<string, string>();
|
||||||
string[] templateSegments;
|
|
||||||
var parameters = PageState.UrlParameters.Split('/', StringSplitOptions.RemoveEmptyEntries);
|
var parameters = PageState.UrlParameters.Split('/', StringSplitOptions.RemoveEmptyEntries);
|
||||||
var parameterId = 0;
|
|
||||||
|
|
||||||
if (string.IsNullOrEmpty(parametersTemplate))
|
if (string.IsNullOrEmpty(template))
|
||||||
{
|
{
|
||||||
|
// no template will populate dictionary with generic "parameter#" keys
|
||||||
for (int i = 0; i < parameters.Length; i++)
|
for (int i = 0; i < parameters.Length; i++)
|
||||||
{
|
{
|
||||||
urlParameters.TryAdd("parameter" + i, parameters[i]);
|
urlParameters.TryAdd("parameter" + i, parameters[i]);
|
||||||
|
@ -185,32 +195,30 @@ namespace Oqtane.Modules
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
templateSegments = parametersTemplate.Split('/', StringSplitOptions.RemoveEmptyEntries);
|
var segments = template.Split('/', StringSplitOptions.RemoveEmptyEntries);
|
||||||
|
string key;
|
||||||
|
|
||||||
if (parameters.Length == templateSegments.Length)
|
|
||||||
{
|
|
||||||
for (int i = 0; i < parameters.Length; i++)
|
for (int i = 0; i < parameters.Length; i++)
|
||||||
{
|
{
|
||||||
if (parameters.Length > i)
|
if (i < segments.Length)
|
||||||
{
|
{
|
||||||
if (templateSegments[i] == parameters[i])
|
key = segments[i];
|
||||||
|
if (key.StartsWith("{") && key.EndsWith("}"))
|
||||||
{
|
{
|
||||||
urlParameters.TryAdd("parameter" + parameterId, parameters[i]);
|
// dynamic segment
|
||||||
parameterId++;
|
key = key.Substring(1, key.Length - 2);
|
||||||
}
|
|
||||||
else if (templateSegments[i].StartsWith("{") && templateSegments[i].EndsWith("}"))
|
|
||||||
{
|
|
||||||
var key = templateSegments[i].Replace("{", "");
|
|
||||||
key = key.Replace("}", "");
|
|
||||||
urlParameters.TryAdd(key, parameters[i]);
|
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
i = parameters.Length;
|
// static segments use generic "parameter#" keys
|
||||||
urlParameters.Clear();
|
key = "parameter" + i.ToString();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
else // unspecified segments use generic "parameter#" keys
|
||||||
|
{
|
||||||
|
key = "parameter" + i.ToString();
|
||||||
}
|
}
|
||||||
|
urlParameters.TryAdd(key, parameters[i]);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -38,13 +38,13 @@ namespace Oqtane.Models
|
||||||
if (pos != -1)
|
if (pos != -1)
|
||||||
{
|
{
|
||||||
UrlParameters = PagePath.Substring(pos + 3);
|
UrlParameters = PagePath.Substring(pos + 3);
|
||||||
PagePath = PagePath.Substring(1, pos);
|
PagePath = PagePath.Substring(0, pos);
|
||||||
}
|
}
|
||||||
pos = PagePath.IndexOf("/" + Constants.ModuleDelimiter + "/");
|
pos = PagePath.IndexOf("/" + Constants.ModuleDelimiter + "/");
|
||||||
if (pos != -1)
|
if (pos != -1)
|
||||||
{
|
{
|
||||||
ModuleId = PagePath.Substring(pos + 3);
|
ModuleId = PagePath.Substring(pos + 3);
|
||||||
PagePath = PagePath.Substring(1, pos);
|
PagePath = PagePath.Substring(0, pos);
|
||||||
}
|
}
|
||||||
if (ModuleId.Length != 0)
|
if (ModuleId.Length != 0)
|
||||||
{
|
{
|
||||||
|
|
Loading…
Reference in New Issue
Block a user