有谁知道为 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/