Replace querystring routes with conventional routes
This commit is contained in:
parent
1c0d2de9fe
commit
32a2d164c3
|
@ -26,325 +26,339 @@
|
|||
|
||||
@code {
|
||||
|
||||
[CascadingParameter] PageState PageState { get; set; }
|
||||
[CascadingParameter] PageState PageState { get; set; }
|
||||
|
||||
[Parameter] Action<PageState> OnStateChange { get; set; }
|
||||
[Parameter] Action<PageState> OnStateChange { get; set; }
|
||||
|
||||
PageState pagestate;
|
||||
RenderFragment DynamicComponent { get; set; }
|
||||
PageState pagestate;
|
||||
RenderFragment DynamicComponent { get; set; }
|
||||
|
||||
string _absoluteUri;
|
||||
bool _navigationInterceptionEnabled;
|
||||
string _absoluteUri;
|
||||
bool _navigationInterceptionEnabled;
|
||||
|
||||
protected override void OnInit()
|
||||
{
|
||||
_absoluteUri = UriHelper.GetAbsoluteUri();
|
||||
UriHelper.OnLocationChanged += OnLocationChanged;
|
||||
|
||||
DynamicComponent = builder =>
|
||||
protected override void OnInit()
|
||||
{
|
||||
if (pagestate != null)
|
||||
_absoluteUri = UriHelper.GetAbsoluteUri();
|
||||
UriHelper.OnLocationChanged += OnLocationChanged;
|
||||
|
||||
DynamicComponent = builder =>
|
||||
{
|
||||
builder.OpenComponent(0, Type.GetType(Constants.DefaultPage));
|
||||
builder.CloseComponent();
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
UriHelper.OnLocationChanged -= OnLocationChanged;
|
||||
}
|
||||
|
||||
protected override async Task OnParametersSetAsync()
|
||||
{
|
||||
if (PageState == null)
|
||||
{
|
||||
await Refresh();
|
||||
}
|
||||
}
|
||||
|
||||
private async Task Refresh()
|
||||
{
|
||||
List<ModuleDefinition> moduledefinitions;
|
||||
List<Models.Theme> themes;
|
||||
List<Alias> aliases;
|
||||
Alias alias;
|
||||
Site site;
|
||||
List<Page> pages;
|
||||
Page page;
|
||||
User user;
|
||||
List<Module> modules;
|
||||
|
||||
bool reload = false;
|
||||
if (PageState == null)
|
||||
{
|
||||
aliases = await AliasService.GetAliasesAsync();
|
||||
alias = null;
|
||||
}
|
||||
else
|
||||
{
|
||||
aliases = PageState.Aliases;
|
||||
alias = PageState.Alias;
|
||||
}
|
||||
if (alias == null || GetAlias(_absoluteUri, aliases).Name != alias.Name)
|
||||
{
|
||||
alias = GetAlias(_absoluteUri, aliases);
|
||||
SiteState.Alias = alias; // set state for services
|
||||
reload = true;
|
||||
}
|
||||
if (PageState == null || reload == true)
|
||||
{
|
||||
moduledefinitions = await ModuleDefinitionService.GetModuleDefinitionsAsync();
|
||||
themes = await ThemeService.GetThemesAsync();
|
||||
site = await SiteService.GetSiteAsync(alias.SiteId);
|
||||
}
|
||||
else
|
||||
{
|
||||
moduledefinitions = PageState.ModuleDefinitions;
|
||||
themes = PageState.Themes;
|
||||
site = PageState.Site;
|
||||
}
|
||||
if (site != null || reload == true)
|
||||
{
|
||||
string path = new Uri(_absoluteUri).PathAndQuery.Substring(1);
|
||||
if (path.EndsWith("/")) { path = path.Substring(0, path.Length - 1); }
|
||||
if (alias.Path != "")
|
||||
{
|
||||
path = path.Replace(alias.Path, "");
|
||||
if (path.StartsWith("/")) { path = path.Substring(1); }
|
||||
}
|
||||
Dictionary<string, string> querystring = ParseQueryString(path);
|
||||
|
||||
if (querystring.ContainsKey("reload"))
|
||||
{
|
||||
reload = true;
|
||||
}
|
||||
|
||||
user = null;
|
||||
if (PageState == null || reload == true)
|
||||
{
|
||||
var authState = await AuthenticationStateProvider.GetAuthenticationStateAsync();
|
||||
if (authState.User.Identity.IsAuthenticated)
|
||||
if (pagestate != null)
|
||||
{
|
||||
user = await UserService.GetUserAsync(authState.User.Identity.Name);
|
||||
builder.OpenComponent(0, Type.GetType(Constants.DefaultPage));
|
||||
builder.CloseComponent();
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
UriHelper.OnLocationChanged -= OnLocationChanged;
|
||||
}
|
||||
|
||||
protected override async Task OnParametersSetAsync()
|
||||
{
|
||||
if (PageState == null)
|
||||
{
|
||||
await Refresh();
|
||||
}
|
||||
}
|
||||
|
||||
private async Task Refresh()
|
||||
{
|
||||
List<ModuleDefinition> moduledefinitions;
|
||||
List<Models.Theme> themes;
|
||||
List<Alias> aliases;
|
||||
Alias alias;
|
||||
Site site;
|
||||
List<Page> pages;
|
||||
Page page;
|
||||
User user;
|
||||
List<Module> modules;
|
||||
int moduleid = -1;
|
||||
string control = "";
|
||||
|
||||
bool reload = false;
|
||||
if (PageState == null)
|
||||
{
|
||||
aliases = await AliasService.GetAliasesAsync();
|
||||
alias = null;
|
||||
}
|
||||
else
|
||||
{
|
||||
user = PageState.User;
|
||||
aliases = PageState.Aliases;
|
||||
alias = PageState.Alias;
|
||||
}
|
||||
|
||||
if (PageState == null || reload == true)
|
||||
if (alias == null || GetAlias(_absoluteUri, aliases).Name != alias.Name)
|
||||
{
|
||||
pages = await PageService.GetPagesAsync(site.SiteId);
|
||||
}
|
||||
else
|
||||
{
|
||||
pages = PageState.Pages;
|
||||
}
|
||||
|
||||
if (path.IndexOf("?") != -1)
|
||||
{
|
||||
path = path.Substring(0, path.IndexOf("?"));
|
||||
}
|
||||
|
||||
if (PageState == null || reload == true)
|
||||
{
|
||||
page = pages.Where(item => item.Path == path).FirstOrDefault();
|
||||
}
|
||||
else
|
||||
{
|
||||
page = PageState.Page;
|
||||
}
|
||||
if (page.Path != path)
|
||||
{
|
||||
page = pages.Where(item => item.Path == path).FirstOrDefault();
|
||||
alias = GetAlias(_absoluteUri, aliases);
|
||||
SiteState.Alias = alias; // set state for services
|
||||
reload = true;
|
||||
}
|
||||
|
||||
if (page != null)
|
||||
if (PageState == null || reload == true)
|
||||
{
|
||||
// check if user is authorized to view page
|
||||
if (UserService.IsAuthorized(user, page.ViewPermissions))
|
||||
moduledefinitions = await ModuleDefinitionService.GetModuleDefinitionsAsync();
|
||||
themes = await ThemeService.GetThemesAsync();
|
||||
site = await SiteService.GetSiteAsync(alias.SiteId);
|
||||
}
|
||||
else
|
||||
{
|
||||
moduledefinitions = PageState.ModuleDefinitions;
|
||||
themes = PageState.Themes;
|
||||
site = PageState.Site;
|
||||
}
|
||||
if (site != null || reload == true)
|
||||
{
|
||||
string path = new Uri(_absoluteUri).PathAndQuery.Substring(1);
|
||||
if (path.EndsWith("/")) { path = path.Substring(0, path.Length - 1); }
|
||||
if (alias.Path != "")
|
||||
{
|
||||
pagestate = new PageState();
|
||||
pagestate.ModuleDefinitions = moduledefinitions;
|
||||
pagestate.Themes = themes;
|
||||
pagestate.Aliases = aliases;
|
||||
pagestate.Alias = alias;
|
||||
pagestate.Site = site;
|
||||
pagestate.Pages = pages;
|
||||
pagestate.Page = page;
|
||||
pagestate.User = user;
|
||||
pagestate.Uri = new Uri(_absoluteUri, UriKind.Absolute);
|
||||
pagestate.QueryString = querystring;
|
||||
pagestate.ModuleId = -1;
|
||||
pagestate.Control = "";
|
||||
path = path.Replace(alias.Path, "");
|
||||
if (path.StartsWith("/")) { path = path.Substring(1); }
|
||||
}
|
||||
Dictionary<string, string> querystring = ParseQueryString(path);
|
||||
|
||||
if (querystring.ContainsKey("mid"))
|
||||
{
|
||||
pagestate.ModuleId = int.Parse(querystring["mid"]);
|
||||
}
|
||||
if (querystring.ContainsKey("ctl"))
|
||||
{
|
||||
pagestate.Control = querystring["ctl"];
|
||||
}
|
||||
if (PageState != null && (PageState.ModuleId != pagestate.ModuleId || PageState.Control != pagestate.Control))
|
||||
{
|
||||
reload = true;
|
||||
}
|
||||
if (querystring.ContainsKey("reload"))
|
||||
{
|
||||
reload = true;
|
||||
}
|
||||
|
||||
if (PageState == null || reload == true)
|
||||
user = null;
|
||||
if (PageState == null || reload == true)
|
||||
{
|
||||
var authState = await AuthenticationStateProvider.GetAuthenticationStateAsync();
|
||||
if (authState.User.Identity.IsAuthenticated)
|
||||
{
|
||||
modules = await ModuleService.GetModulesAsync(page.PageId);
|
||||
modules = ProcessModules(modules, moduledefinitions, pagestate.Control, page.Panes);
|
||||
user = await UserService.GetUserAsync(authState.User.Identity.Name);
|
||||
}
|
||||
else
|
||||
{
|
||||
modules = PageState.Modules;
|
||||
}
|
||||
pagestate.Modules = modules;
|
||||
|
||||
OnStateChange?.Invoke(pagestate);
|
||||
}
|
||||
else
|
||||
{
|
||||
// user is not authorized to view page
|
||||
user = PageState.User;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
// page does not exist
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
// site does not exist
|
||||
}
|
||||
}
|
||||
|
||||
private async void OnLocationChanged(object sender, LocationChangedEventArgs args)
|
||||
{
|
||||
_absoluteUri = args.Location;
|
||||
await Refresh();
|
||||
}
|
||||
|
||||
Task IHandleAfterRender.OnAfterRenderAsync()
|
||||
{
|
||||
if (!_navigationInterceptionEnabled && ComponentContext.IsConnected)
|
||||
{
|
||||
_navigationInterceptionEnabled = true;
|
||||
return NavigationInterception.EnableNavigationInterceptionAsync();
|
||||
}
|
||||
return Task.CompletedTask;
|
||||
}
|
||||
|
||||
private Dictionary<string, string> ParseQueryString(string path)
|
||||
{
|
||||
Dictionary<string, string> querystring = new Dictionary<string, string>();
|
||||
if (path.IndexOf("?") != -1)
|
||||
{
|
||||
foreach (string kvp in path.Substring(path.IndexOf("?") + 1).Split('&'))
|
||||
{
|
||||
if (kvp != "")
|
||||
if (PageState == null || reload == true)
|
||||
{
|
||||
if (kvp.Contains("="))
|
||||
pages = await PageService.GetPagesAsync(site.SiteId);
|
||||
}
|
||||
else
|
||||
{
|
||||
pages = PageState.Pages;
|
||||
}
|
||||
|
||||
// format path so that it can be located in the pages collection
|
||||
if (path.IndexOf("?") != -1)
|
||||
{
|
||||
// remove querystring from path
|
||||
path = path.Substring(0, path.IndexOf("?"));
|
||||
}
|
||||
// remove admin route elements from path
|
||||
string[] segments = path.Split('/');
|
||||
int result;
|
||||
// admin routes are in the form of path/moduleid/control or path/moduleid
|
||||
if (segments.Length >= 2 && int.TryParse(segments[segments.Length - 2], out result))
|
||||
{
|
||||
control = segments[segments.Length - 1];
|
||||
moduleid = result;
|
||||
path = path.Replace("/" + moduleid.ToString() + "/" + control, "");
|
||||
}
|
||||
else
|
||||
{
|
||||
if (segments.Length >= 1 && int.TryParse(segments[segments.Length - 1], out result))
|
||||
{
|
||||
string[] pair = kvp.Split('=');
|
||||
querystring.Add(pair[0], pair[1]);
|
||||
moduleid = result;
|
||||
path = path.Replace("/" + moduleid.ToString(), "");
|
||||
}
|
||||
}
|
||||
|
||||
if (PageState == null || reload == true)
|
||||
{
|
||||
page = pages.Where(item => item.Path == path).FirstOrDefault();
|
||||
}
|
||||
else
|
||||
{
|
||||
page = PageState.Page;
|
||||
}
|
||||
if (page.Path != path)
|
||||
{
|
||||
page = pages.Where(item => item.Path == path).FirstOrDefault();
|
||||
reload = true;
|
||||
}
|
||||
|
||||
if (page != null)
|
||||
{
|
||||
// check if user is authorized to view page
|
||||
if (UserService.IsAuthorized(user, page.ViewPermissions))
|
||||
{
|
||||
pagestate = new PageState();
|
||||
pagestate.ModuleDefinitions = moduledefinitions;
|
||||
pagestate.Themes = themes;
|
||||
pagestate.Aliases = aliases;
|
||||
pagestate.Alias = alias;
|
||||
pagestate.Site = site;
|
||||
pagestate.Pages = pages;
|
||||
pagestate.Page = page;
|
||||
pagestate.User = user;
|
||||
pagestate.Uri = new Uri(_absoluteUri, UriKind.Absolute);
|
||||
pagestate.QueryString = querystring;
|
||||
pagestate.ModuleId = moduleid;
|
||||
pagestate.Control = control;
|
||||
|
||||
if (PageState != null && (PageState.ModuleId != pagestate.ModuleId || PageState.Control != pagestate.Control))
|
||||
{
|
||||
reload = true;
|
||||
}
|
||||
|
||||
if (PageState == null || reload == true)
|
||||
{
|
||||
modules = await ModuleService.GetModulesAsync(page.PageId);
|
||||
modules = ProcessModules(modules, moduledefinitions, pagestate.Control, page.Panes);
|
||||
}
|
||||
else
|
||||
{
|
||||
modules = PageState.Modules;
|
||||
}
|
||||
pagestate.Modules = modules;
|
||||
|
||||
OnStateChange?.Invoke(pagestate);
|
||||
}
|
||||
else
|
||||
{
|
||||
querystring.Add(kvp, "true"); // default querystring when no value is provided
|
||||
// user is not authorized to view page
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return querystring;
|
||||
}
|
||||
|
||||
private List<Module> ProcessModules(List<Module> modules, List<ModuleDefinition> moduledefinitions, string control, string panes)
|
||||
{
|
||||
ModuleDefinition moduledefinition;
|
||||
|
||||
if (control == "")
|
||||
{
|
||||
control = Constants.DefaultControl;
|
||||
}
|
||||
|
||||
Dictionary<string, int> paneindex = new Dictionary<string, int>();
|
||||
foreach (Module module in modules)
|
||||
{
|
||||
// set the type based on the template and action
|
||||
moduledefinition = moduledefinitions.Where(item => item.ModuleDefinitionName == module.ModuleDefinitionName).FirstOrDefault();
|
||||
if (moduledefinition != null)
|
||||
{
|
||||
string typename = moduledefinition.ControlTypeTemplate;
|
||||
if (moduledefinition.ControlTypeRoutes != "")
|
||||
else
|
||||
{
|
||||
foreach (string route in moduledefinition.ControlTypeRoutes.Split(';'))
|
||||
{
|
||||
if (route.StartsWith(control + "="))
|
||||
{
|
||||
typename = route.Replace(control + "=", "");
|
||||
}
|
||||
}
|
||||
// page does not exist
|
||||
}
|
||||
module.ModuleType = typename.Replace("{Control}", control);
|
||||
}
|
||||
|
||||
// ensure module's pane exists in current page and if not, assign it to the Admin pane
|
||||
if (!panes.Contains(module.Pane))
|
||||
{
|
||||
module.Pane = Constants.AdminPane;
|
||||
}
|
||||
|
||||
// calculate module position within pane
|
||||
if (paneindex.ContainsKey(module.Pane))
|
||||
{
|
||||
paneindex[module.Pane] += 1;
|
||||
}
|
||||
else
|
||||
{
|
||||
paneindex.Add(module.Pane, 0);
|
||||
// site does not exist
|
||||
}
|
||||
module.PaneModuleIndex = paneindex[module.Pane];
|
||||
}
|
||||
|
||||
foreach (Module module in modules)
|
||||
private async void OnLocationChanged(object sender, LocationChangedEventArgs args)
|
||||
{
|
||||
module.PaneModuleCount = paneindex[module.Pane] + 1;
|
||||
_absoluteUri = args.Location;
|
||||
await Refresh();
|
||||
}
|
||||
return modules;
|
||||
}
|
||||
|
||||
private Alias GetAlias(string absoluteUri, List<Alias> aliases)
|
||||
{
|
||||
Task IHandleAfterRender.OnAfterRenderAsync()
|
||||
{
|
||||
if (!_navigationInterceptionEnabled && ComponentContext.IsConnected)
|
||||
{
|
||||
_navigationInterceptionEnabled = true;
|
||||
return NavigationInterception.EnableNavigationInterceptionAsync();
|
||||
}
|
||||
return Task.CompletedTask;
|
||||
}
|
||||
|
||||
string aliasname;
|
||||
Alias alias = null;
|
||||
Uri uri = new Uri(absoluteUri);
|
||||
private Dictionary<string, string> ParseQueryString(string path)
|
||||
{
|
||||
Dictionary<string, string> querystring = new Dictionary<string, string>();
|
||||
if (path.IndexOf("?") != -1)
|
||||
{
|
||||
foreach (string kvp in path.Substring(path.IndexOf("?") + 1).Split('&'))
|
||||
{
|
||||
if (kvp != "")
|
||||
{
|
||||
if (kvp.Contains("="))
|
||||
{
|
||||
string[] pair = kvp.Split('=');
|
||||
querystring.Add(pair[0], pair[1]);
|
||||
}
|
||||
else
|
||||
{
|
||||
querystring.Add(kvp, "true"); // default querystring when no value is provided
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return querystring;
|
||||
}
|
||||
|
||||
if (uri.Segments.Count() > 1)
|
||||
private List<Module> ProcessModules(List<Module> modules, List<ModuleDefinition> moduledefinitions, string control, string panes)
|
||||
{
|
||||
// check if first path segment is an alias ( ie. a subfolder - www.domain.com/subfolder )
|
||||
aliasname = uri.Authority + "/" + uri.Segments[1];
|
||||
if (aliasname.EndsWith("/")) { aliasname = aliasname.Substring(0, aliasname.Length - 1); }
|
||||
alias = aliases.Where(item => item.Name == aliasname).FirstOrDefault();
|
||||
ModuleDefinition moduledefinition;
|
||||
|
||||
if (control == "")
|
||||
{
|
||||
control = Constants.DefaultControl;
|
||||
}
|
||||
|
||||
Dictionary<string, int> paneindex = new Dictionary<string, int>();
|
||||
foreach (Module module in modules)
|
||||
{
|
||||
// set the type based on the template and action
|
||||
moduledefinition = moduledefinitions.Where(item => item.ModuleDefinitionName == module.ModuleDefinitionName).FirstOrDefault();
|
||||
if (moduledefinition != null)
|
||||
{
|
||||
string typename = moduledefinition.ControlTypeTemplate;
|
||||
if (moduledefinition.ControlTypeRoutes != "")
|
||||
{
|
||||
foreach (string route in moduledefinition.ControlTypeRoutes.Split(';'))
|
||||
{
|
||||
if (route.StartsWith(control + "="))
|
||||
{
|
||||
typename = route.Replace(control + "=", "");
|
||||
}
|
||||
}
|
||||
}
|
||||
module.ModuleType = typename.Replace("{Control}", control);
|
||||
}
|
||||
|
||||
// ensure module's pane exists in current page and if not, assign it to the Admin pane
|
||||
if (!panes.Contains(module.Pane))
|
||||
{
|
||||
module.Pane = Constants.AdminPane;
|
||||
}
|
||||
|
||||
// calculate module position within pane
|
||||
if (paneindex.ContainsKey(module.Pane))
|
||||
{
|
||||
paneindex[module.Pane] += 1;
|
||||
}
|
||||
else
|
||||
{
|
||||
paneindex.Add(module.Pane, 0);
|
||||
}
|
||||
module.PaneModuleIndex = paneindex[module.Pane];
|
||||
}
|
||||
|
||||
foreach (Module module in modules)
|
||||
{
|
||||
module.PaneModuleCount = paneindex[module.Pane] + 1;
|
||||
}
|
||||
return modules;
|
||||
}
|
||||
if (alias == null)
|
||||
|
||||
private Alias GetAlias(string absoluteUri, List<Alias> aliases)
|
||||
{
|
||||
aliasname = uri.Authority;
|
||||
alias = aliases.Where(item => item.Name == aliasname).FirstOrDefault();
|
||||
|
||||
string aliasname;
|
||||
Alias alias = null;
|
||||
Uri uri = new Uri(absoluteUri);
|
||||
|
||||
if (uri.Segments.Count() > 1)
|
||||
{
|
||||
// check if first path segment is an alias ( ie. a subfolder - www.domain.com/subfolder )
|
||||
aliasname = uri.Authority + "/" + uri.Segments[1];
|
||||
if (aliasname.EndsWith("/")) { aliasname = aliasname.Substring(0, aliasname.Length - 1); }
|
||||
alias = aliases.Where(item => item.Name == aliasname).FirstOrDefault();
|
||||
}
|
||||
if (alias == null)
|
||||
{
|
||||
aliasname = uri.Authority;
|
||||
alias = aliases.Where(item => item.Name == aliasname).FirstOrDefault();
|
||||
}
|
||||
if (alias == null && aliases.Count > 0)
|
||||
{
|
||||
// use first alias if Uri does not exist
|
||||
alias = aliases.FirstOrDefault();
|
||||
}
|
||||
alias.Scheme = uri.Scheme;
|
||||
return alias;
|
||||
}
|
||||
if (alias == null && aliases.Count > 0)
|
||||
{
|
||||
// use first alias if Uri does not exist
|
||||
alias = aliases.FirstOrDefault();
|
||||
}
|
||||
alias.Scheme = uri.Scheme;
|
||||
return alias;
|
||||
}
|
||||
|
||||
}
|
|
@ -44,14 +44,19 @@ namespace Oqtane.Shared
|
|||
|
||||
public static string EditUrl(PageState pagestate, Module modulestate, string action, string parameters)
|
||||
{
|
||||
string url = pagestate.Alias.Path + "/" + pagestate.Page.Path + "?mid=" + modulestate.ModuleId.ToString();
|
||||
string url = pagestate.Alias.Path;
|
||||
if (pagestate.Page.Path != "")
|
||||
{
|
||||
url += "/" + pagestate.Page.Path;
|
||||
}
|
||||
url += "/" + modulestate.ModuleId.ToString();
|
||||
if (action != "")
|
||||
{
|
||||
url += "&ctl=" + action;
|
||||
url += "/" + action;
|
||||
}
|
||||
if (!string.IsNullOrEmpty(parameters))
|
||||
{
|
||||
url += "&" + parameters;
|
||||
url += "?" + parameters;
|
||||
}
|
||||
return url;
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue
Block a user