From 15d5c7a669f024e7be6b2819d915b2cd0fdec421 Mon Sep 17 00:00:00 2001 From: Samuele Locatelli Date: Fri, 23 Feb 2024 11:02:17 +0100 Subject: [PATCH] Aggiunto helper estrazione querystring --- .../Pages/TestComponenti.razor | 49 ++++++++++++++----- EgwCoreLib.Razor/EgwCoreLib.Razor.csproj | 1 + .../NavigationManagerExtension.cs | 44 +++++++++++++++++ 3 files changed, 82 insertions(+), 12 deletions(-) create mode 100644 EgwCoreLib.Razor/NavigationManagerExtension.cs diff --git a/EgwCoreLib.BlazorTest/Pages/TestComponenti.razor b/EgwCoreLib.BlazorTest/Pages/TestComponenti.razor index 7146be5..d108886 100644 --- a/EgwCoreLib.BlazorTest/Pages/TestComponenti.razor +++ b/EgwCoreLib.BlazorTest/Pages/TestComponenti.razor @@ -1,11 +1,9 @@ @page "/TestComponenti" +@inject NavigationManager NavMan + Test - - - -

Test Componenti custom

@@ -15,7 +13,7 @@
-

Test Image

+ Test Image
@@ -48,25 +46,43 @@
- Clipboard copy + Input Speciali
Test copia clipboard, GUID (presudo)random +
+ Input numerico con gestione cifre (es x double) + +
+ @valDecimale +
- Number Input + QueryString
- Input numerico con gestione cifre (es x double) - -
- @valDecimale -
+ + + Test valori QueryString +
    +
  • + Query string Nome: +
  • +
  • + @qsNome +
  • +
  • + Query string Intero: +
  • +
  • + @qsIntero +
  • +
@@ -81,4 +97,13 @@ protected string textToCopy = $"{Guid.NewGuid()}"; protected decimal valDecimale = 12345; + private string qsNome = ""; + private int qsIntero = 0; + + protected override void OnInitialized() + { + // base.OnInitialized(); + qsNome = NavMan.ExtractQueryStringByKey("Nome"); + qsIntero = NavMan.ExtractQueryStringByKey("Intero"); + } } diff --git a/EgwCoreLib.Razor/EgwCoreLib.Razor.csproj b/EgwCoreLib.Razor/EgwCoreLib.Razor.csproj index b5c09af..1187007 100644 --- a/EgwCoreLib.Razor/EgwCoreLib.Razor.csproj +++ b/EgwCoreLib.Razor/EgwCoreLib.Razor.csproj @@ -68,6 +68,7 @@ + diff --git a/EgwCoreLib.Razor/NavigationManagerExtension.cs b/EgwCoreLib.Razor/NavigationManagerExtension.cs new file mode 100644 index 0000000..cddccbd --- /dev/null +++ b/EgwCoreLib.Razor/NavigationManagerExtension.cs @@ -0,0 +1,44 @@ +using Microsoft.AspNetCore.Components; +using Microsoft.AspNetCore.WebUtilities; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace EgwCoreLib.Razor +{ + public static class NavigationManagerExtension + { + #region Public Methods + + /// + /// Estensione metodo NavigationManager + /// + /// https://code-maze.com/query-strings-blazor-webassembly/ + /// + /// + /// + /// + /// + public static T ExtractQueryStringByKey(this NavigationManager navManager, string key) + { + var uri = navManager.ToAbsoluteUri(navManager.Uri); + QueryHelpers.ParseQuery(uri.Query) + .TryGetValue(key, out var queryValue); + + if (typeof(T).Equals(typeof(int))) + { + int.TryParse(queryValue, out int result); + return (T)(object)result; + } + + if (typeof(T).Equals(typeof(string))) + return (T)(object)queryValue.ToString(); + + return default; + } + + #endregion Public Methods + } +}