Does anyone know of a way to generate an xml sitemap for a blazor application? tried producing one through many of the generator tools available online but none seem to produce an accurate xml sitemap
Does anyone know of a way to generate an xml sitemap for a blazor application? tried producing one through many of the generator tools available online but none seem to produce an accurate xml sitemap
With the help of code @Vi100 I created a class called Site Map and call it in startup.cs file.
Sitemap.cs
using Microsoft.AspNetCore.Components;
using Microsoft.AspNetCore.Http;
using System.Linq;
using System.Reflection;
using System.Threading.Tasks;
public class Sitemap {
public static async Task Generate(HttpContext context) {
var pages = Assembly.GetExecutingAssembly().ExportedTypes.Where(p =>
p.IsSubclassOf(typeof(ComponentBase)) && p.Namespace == "your namespace.Pages");
foreach(var page in pages) {
// Do whatever you want with the page components, e.g. show it's name:
await context.Response.WriteAsync(page.FullName + "\n");
// From here you can access most part of the static info about the
//component,
// but if you want to get the page titles or similar info, you'll have to
//create
// some custom attributes and decorate the pages with them.
}
}
}
and change startup.cs
app.UseEndpoints(endpoints => {
endpoints.MapGet("/sitemap.xml", async context => {
await Sitemap.Generate(context);
});
endpoints.MapBlazorHub();
endpoints.MapFallbackToPage("/_Host");
});
app.UseEndpoints(endpoints =>
{
endpoints.MapControllers();
endpoints.MapHub<ConnectionHub>("/ConnectionHub");
endpoints.MapGet("/sitemap.xml", async context =>
{
await context.Response.WriteAsync(SitemapService.GetSitemap());
});
endpoints.MapBlazorHub();
endpoints.MapFallbackToPage("/_Host");
endpoints.MapControllerRoute("default", "{controller=Home}/{action=Index}/{id?}");
});
The line enpoints.MapGet
You CAN'T have a sitemap from a Blazor-server application. If what you pretend is having a sitemap in a traditional way for declaring how to navigate across the pages of your application you're in a bad luck, since the blazor app is served as a single page application, exposing only one url as an entry point, the one corresponding to the route enpoint you have defined for the "_Host.cshtml" file...
If I'm wrong in my previous assumption and you want some kind of internal sitemap for any reason then you could use reflection, to iterate over all the pages in your app and construct whatever document format you need about them:
@page "/"
@using System.Linq;
@using System.Reflection;
<button @onclick="@SearchForPages">Search for pages</button>
@code{
private void SearchForPages()
{
// Get all the components derived from ComponentBase that are in the "YourAppName.Pages" namespace
// (adjust filter conditions if necessary...)
var pages = Assembly.GetExecutingAssembly()
.ExportedTypes
.Where(p => p.IsSubclassOf(typeof(ComponentBase)) && p.Namespace == "YourAppName.Pages");
foreach (var page in pages)
{
// Do whatever you want with the page components, e.g. show it's name:
Console.WriteLine(component.ToString());
// From here you can access most part of the static info about the component,
// but if you want to get the page titles or similar info, you'll have to create
// some custom attributes and decorate the pages with them.
}
}
}
Hope it helps