Aggiunto helper estrazione querystring
This commit is contained in:
@@ -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
|
||||
|
||||
/// <summary>
|
||||
/// Estensione metodo NavigationManager
|
||||
///
|
||||
/// https://code-maze.com/query-strings-blazor-webassembly/
|
||||
/// </summary>
|
||||
/// <typeparam name="T"></typeparam>
|
||||
/// <param name="navManager"></param>
|
||||
/// <param name="key"></param>
|
||||
/// <returns></returns>
|
||||
public static T ExtractQueryStringByKey<T>(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
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user