c# - 如何为服务器端 Blazor 应用程序生成 xml 站点地图

标签 c# asp.net-core blazor blazor-server-side

有谁知道为 blazor 应用程序生成 xml 站点地图的方法吗?尝试通过许多在线可用的生成器工具生成一个,但似乎没有一个生成准确的 xml 站点地图

最佳答案

在代码@Vi100 的帮助下,我创建了一个名为 Site Map 的类,并在 startup.cs 文件中调用它。

站点地图.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.
        }
    }
}

并更改startup.cs

app.UseEndpoints(endpoints => {
    endpoints.MapGet("/sitemap.xml", async context => {
        await Sitemap.Generate(context);
    });
    endpoints.MapBlazorHub();
    endpoints.MapFallbackToPage("/_Host");
});

关于c# - 如何为服务器端 Blazor 应用程序生成 xml 站点地图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62551206/

相关文章:

c# - 如何在 JSON 响应 ASP.NET Core 中关闭或处理 camelCasing?

syncfusion - Blazor 问题呈现从 Web Api 返回的数据

c# - 为什么我得到 ImageSharp 不支持同步读取?

c# - TextBox 水平滚动条在 Windows 窗体中无法正常工作

c# - WinRT 中 ViewModel 层的虚拟化

c# - 将大量数据从 C# 导出到 excel 的最佳/最快方法是什么

ubuntu - ASP.Net Core 应用服务在 Ubuntu 上只监听 5000 端口

c# - 在 ConfigureServices(IServiceCollection services) 中调用 services.AddOptions() 不明确

c# - 应用程序退出时显示等待消息

asp.net - 没有 Duende 身份服务器的 Blazor