|
| 1 | +using System.Reflection; |
| 2 | +using Microsoft.AspNetCore.Components; |
| 3 | + |
| 4 | +namespace Hedin.UI.Demo.Services; |
| 5 | + |
| 6 | +public class SeoService |
| 7 | +{ |
| 8 | + private readonly IHttpContextAccessor _httpContextAccessor; |
| 9 | + private SeoMetadata _currentMetadata = new(); |
| 10 | + |
| 11 | + public SeoService(IHttpContextAccessor httpContextAccessor) |
| 12 | + { |
| 13 | + _httpContextAccessor = httpContextAccessor; |
| 14 | + } |
| 15 | + |
| 16 | + public SeoMetadata CurrentMetadata => _currentMetadata; |
| 17 | + |
| 18 | + public void SetMetadata(SeoMetadata metadata) |
| 19 | + { |
| 20 | + _currentMetadata = metadata; |
| 21 | + } |
| 22 | + |
| 23 | + public void SetMetadata(string? title = null, string? description = null, string? keywords = null, |
| 24 | + string? image = null, string? url = null, string? type = "website") |
| 25 | + { |
| 26 | + _currentMetadata = new SeoMetadata |
| 27 | + { |
| 28 | + Title = title ?? _currentMetadata.Title, |
| 29 | + Description = description ?? _currentMetadata.Description, |
| 30 | + Keywords = keywords ?? _currentMetadata.Keywords, |
| 31 | + Image = image ?? _currentMetadata.Image, |
| 32 | + Url = url ?? GetCurrentUrl(), |
| 33 | + Type = type |
| 34 | + }; |
| 35 | + } |
| 36 | + |
| 37 | + public string GetCurrentUrl() |
| 38 | + { |
| 39 | + var request = _httpContextAccessor.HttpContext?.Request; |
| 40 | + if (request == null) return string.Empty; |
| 41 | + |
| 42 | + var scheme = request.Scheme; |
| 43 | + var host = request.Host; |
| 44 | + var path = request.Path; |
| 45 | + var query = request.QueryString; |
| 46 | + |
| 47 | + // Ensure we have a valid URL |
| 48 | + var url = $"{scheme}://{host}{path}{query}"; |
| 49 | + return !string.IsNullOrEmpty(url) ? url : GetBaseUrl(); |
| 50 | + } |
| 51 | + |
| 52 | + public string GetBaseUrl() |
| 53 | + { |
| 54 | + var request = _httpContextAccessor.HttpContext?.Request; |
| 55 | + if (request == null) return string.Empty; |
| 56 | + |
| 57 | + var scheme = request.Scheme; |
| 58 | + var host = request.Host; |
| 59 | + |
| 60 | + return $"{scheme}://{host}"; |
| 61 | + } |
| 62 | + |
| 63 | + public List<RouteInfo> GetAllRoutableComponents() |
| 64 | + { |
| 65 | + var routes = new List<RouteInfo>(); |
| 66 | + var assembly = Assembly.GetExecutingAssembly(); |
| 67 | + |
| 68 | + var routableTypes = assembly.GetTypes() |
| 69 | + .Where(t => t.IsSubclassOf(typeof(ComponentBase)) && |
| 70 | + t.GetCustomAttributes<RouteAttribute>().Any()) |
| 71 | + .ToList(); |
| 72 | + |
| 73 | + foreach (var type in routableTypes) |
| 74 | + { |
| 75 | + var routeAttributes = type.GetCustomAttributes<RouteAttribute>(); |
| 76 | + foreach (var routeAttr in routeAttributes) |
| 77 | + { |
| 78 | + routes.Add(new RouteInfo |
| 79 | + { |
| 80 | + Type = type, |
| 81 | + Route = routeAttr.Template, |
| 82 | + Name = type.Name, |
| 83 | + Description = GetComponentDescription(type) |
| 84 | + }); |
| 85 | + } |
| 86 | + } |
| 87 | + |
| 88 | + return routes; |
| 89 | + } |
| 90 | + |
| 91 | + private string GetComponentDescription(Type componentType) |
| 92 | + { |
| 93 | + // Try to get description from XML documentation or attributes |
| 94 | + var descriptionAttr = componentType.GetCustomAttribute<System.ComponentModel.DescriptionAttribute>(); |
| 95 | + if (descriptionAttr != null) |
| 96 | + return descriptionAttr.Description; |
| 97 | + |
| 98 | + // Generate a generic description - pages should set their own specific descriptions |
| 99 | + return $"Hedin UI {componentType.Name} - Component documentation and examples"; |
| 100 | + } |
| 101 | +} |
| 102 | + |
| 103 | +public class SeoMetadata |
| 104 | +{ |
| 105 | + public string Title { get; set; } = "Hedin UI - Build Blazor Apps in Minutes"; |
| 106 | + public string Description { get; set; } = "Hedin UI gives you a polished suite of MudBlazor-based components, themes, and patterns so you can ship UIs faster."; |
| 107 | + public string Keywords { get; set; } = "Blazor, MudBlazor, UI Components, C#, .NET, Web Development, Hedin UI"; |
| 108 | + public string Image { get; set; } = "/HedinUI.svg"; |
| 109 | + public string Url { get; set; } = ""; |
| 110 | + public string Type { get; set; } = "website"; |
| 111 | + public string Author { get; set; } = "Hedin IT"; |
| 112 | + public string SiteName { get; set; } = "Hedin UI"; |
| 113 | + public string Locale { get; set; } = "en_US"; |
| 114 | +} |
| 115 | + |
| 116 | +public class RouteInfo |
| 117 | +{ |
| 118 | + public Type Type { get; set; } = null!; |
| 119 | + public string Route { get; set; } = ""; |
| 120 | + public string Name { get; set; } = ""; |
| 121 | + public string Description { get; set; } = ""; |
| 122 | +} |
0 commit comments