This repository has been archived by the owner on Nov 1, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
991719f
commit 27490e4
Showing
4 changed files
with
101 additions
and
1 deletion.
There are no files selected for viewing
27 changes: 27 additions & 0 deletions
27
src/BitzArt.Blazor.MVVM/Attributes/QueryStringParameterAttribute.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
namespace BitzArt.Blazor.MVVM; | ||
|
||
/// <summary> | ||
/// Attribute to decorate properties that should be read from the query string. | ||
/// </summary> | ||
[AttributeUsage(AttributeTargets.Property, AllowMultiple = false, Inherited = true)] | ||
public sealed class QueryStringParameterAttribute : Attribute | ||
{ | ||
/// <summary>Name of the query string parameter. It uses the property name by default.</summary> | ||
public string? Name { get; } | ||
|
||
/// <summary> | ||
/// Initializes a new instance of the <see cref="QueryStringParameterAttribute"/> class. | ||
/// </summary> | ||
public QueryStringParameterAttribute() | ||
{ | ||
} | ||
|
||
/// <summary> | ||
/// Initializes a new instance of the <see cref="QueryStringParameterAttribute"/> class. | ||
/// </summary> | ||
/// <param name="name"></param> | ||
public QueryStringParameterAttribute(string name) | ||
{ | ||
Name = name; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
56 changes: 56 additions & 0 deletions
56
src/BitzArt.Blazor.MVVM/Extensions/QueryStringParameterExtensions.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
using Microsoft.AspNetCore.Components; | ||
using Microsoft.AspNetCore.WebUtilities; | ||
using Microsoft.Extensions.Primitives; | ||
using System.Globalization; | ||
using System.Reflection; | ||
|
||
namespace BitzArt.Blazor.MVVM; | ||
|
||
internal static class QueryStringParameterExtensions | ||
{ | ||
// Apply the values from the query string to the current component | ||
public static void SetParametersFromQueryString<T>(this T component, NavigationManager navigationManager) | ||
where T : class | ||
{ | ||
if (!Uri.TryCreate(navigationManager.Uri, UriKind.RelativeOrAbsolute, out var uri)) | ||
throw new InvalidOperationException("The current url is not a valid URI. Url: " + navigationManager.Uri); | ||
|
||
// Parse the query string | ||
Dictionary<string, StringValues> queryString = QueryHelpers.ParseQuery(uri.Query); | ||
|
||
// Enumerate all properties of the component | ||
foreach (var property in GetProperties<T>()) | ||
{ | ||
// Get the name of the parameter to read from the query string | ||
var parameterName = GetQueryStringParameterName(property); | ||
if (parameterName == null) | ||
continue; // The property is not decorated by [QueryStringParameterAttribute] | ||
|
||
if (queryString.TryGetValue(parameterName, out var value)) | ||
{ | ||
// Convert the value from string to the actual property type | ||
var convertedValue = ConvertValue(value, property.PropertyType); | ||
property.SetValue(component, convertedValue); | ||
} | ||
} | ||
} | ||
|
||
private static object ConvertValue(StringValues value, Type type) | ||
{ | ||
return Convert.ChangeType(value[0], type, CultureInfo.InvariantCulture)!; | ||
} | ||
|
||
private static PropertyInfo[] GetProperties<T>() | ||
{ | ||
return typeof(T).GetProperties(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance); | ||
} | ||
|
||
private static string? GetQueryStringParameterName(PropertyInfo property) | ||
{ | ||
var attribute = property.GetCustomAttribute<QueryStringParameterAttribute>(); | ||
if (attribute == null) | ||
return null; | ||
|
||
return attribute.Name ?? property.Name; | ||
} | ||
} |