diff --git a/src/Templates/Boilerplate/Bit.Boilerplate/src/Server/Boilerplate.Server.Web/Program.Middlewares.cs b/src/Templates/Boilerplate/Bit.Boilerplate/src/Server/Boilerplate.Server.Web/Program.Middlewares.cs index 518fda1e17..def2f8619d 100644 --- a/src/Templates/Boilerplate/Bit.Boilerplate/src/Server/Boilerplate.Server.Web/Program.Middlewares.cs +++ b/src/Templates/Boilerplate/Bit.Boilerplate/src/Server/Boilerplate.Server.Web/Program.Middlewares.cs @@ -104,6 +104,8 @@ private static void ConfiureMiddlewares(this WebApplication app) QueryStringParameter = queryStringParameter }).WithTags("Test"); + app.UseSiteMap(); + app.MapControllers().RequireAuthorization(); // Handle the rest of requests with blazor @@ -118,6 +120,36 @@ private static void ConfiureMiddlewares(this WebApplication app) } } + private static void UseSiteMap(this WebApplication app) + { + var urls = typeof(Urls) + .GetFields() + .Select(f => f.GetValue(null)!.ToString()!) + .ToList()!; + + urls = CultureInfoManager.MultilingualEnabled ? + CultureInfoManager.SupportedCultures.SelectMany(sc => urls.Select(url => $"{url}?culture={sc.Culture.Name}")).ToList() : + urls; + + const string siteMapHeader = "\r\n"; + + app.MapGet("/sitemap.XML", async context => + { + if (siteMap is null) + { + var baseUrl = context.Request.GetBaseUrl(); + + siteMap = $"{siteMapHeader}{string.Join(Environment.NewLine, urls.Select(u => $"{new Uri(baseUrl, u)}"))}"; + } + + context.Response.Headers.ContentType = "application/xml"; + + await context.Response.WriteAsync(siteMap, context.RequestAborted); + }); + } + + private static string? siteMap; + /// /// Prior to the introduction of .NET 8, the Blazor router effectively managed NotFound and NotAuthorized components during pre-rendering. /// However, the current behavior has changed, and it now exclusively returns 401, 403, and 404 status codes with an empty body response!