Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/316.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何为服务器端Blazor应用程序生成xml站点地图_C#_Asp.net Core_Blazor_Blazor Server Side - Fatal编程技术网

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

C# 如何为服务器端Blazor应用程序生成xml站点地图,c#,asp.net-core,blazor,blazor-server-side,C#,Asp.net Core,Blazor,Blazor Server Side,有人知道为blazor应用程序生成xml站点地图的方法吗?尝试通过许多在线可用的生成器工具生成一个站点地图,但似乎都无法生成准确的xml站点地图Blazor服务器应用程序无法生成站点地图。如果你假装用传统的方式拥有一个站点地图来声明如何在应用程序的页面间导航,那么你就倒霉了,因为blazor应用程序是作为一个单页面应用程序使用的,只公开一个url作为入口点,这个url对应于你为“_Host.cshtml”文件定义的路由点 如果我之前的假设是错误的,并且您出于任何原因需要某种内部站点地图,那么您可

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

Blazor服务器应用程序无法生成站点地图。如果你假装用传统的方式拥有一个站点地图来声明如何在应用程序的页面间导航,那么你就倒霉了,因为blazor应用程序是作为一个单页面应用程序使用的,只公开一个url作为入口点,这个url对应于你为“_Host.cshtml”文件定义的路由点

如果我之前的假设是错误的,并且您出于任何原因需要某种内部站点地图,那么您可以使用反射,在应用程序中的所有页面上迭代,并构建您需要的任何文档格式:

 @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.
         }
     }
} 
@page/“
@使用System.Linq;
@运用系统反思;
搜索网页
@代码{
私有void SearchForPages()
{
//获取从ComponentBase派生的、位于“YourAppName.Pages”命名空间中的所有组件
//(如有必要,调整过滤条件…)
var pages=Assembly.getExecutionGassembly()
.导出类型
其中(p=>p.IsSubclassOf(typeof(ComponentBase))&&p.Namespace==“YourAppName.Pages”);
foreach(页面中的变量页面)
{
//对页面组件执行任何操作,例如显示其名称:
Console.WriteLine(component.ToString());
//从这里您可以访问有关组件的大部分静态信息,
//但是如果你想获得页面标题或类似的信息,你必须创建
//一些自定义属性并用它们装饰页面。
}
}
} 

希望它有帮助

您不能从Blazor服务器应用程序中获得站点地图。如果你假装用传统的方式拥有一个站点地图来声明如何在应用程序的页面间导航,那么你就倒霉了,因为blazor应用程序是作为一个单页面应用程序使用的,只公开一个url作为入口点,这个url对应于你为“_Host.cshtml”文件定义的路由点

如果我之前的假设是错误的,并且您出于任何原因需要某种内部站点地图,那么您可以使用反射,在应用程序中的所有页面上迭代,并构建您需要的任何文档格式:

 @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.
         }
     }
} 
@page/“
@使用System.Linq;
@运用系统反思;
搜索网页
@代码{
私有void SearchForPages()
{
//获取从ComponentBase派生的、位于“YourAppName.Pages”命名空间中的所有组件
//(如有必要,调整过滤条件…)
var pages=Assembly.getExecutionGassembly()
.导出类型
其中(p=>p.IsSubclassOf(typeof(ComponentBase))&&p.Namespace==“YourAppName.Pages”);
foreach(页面中的变量页面)
{
//对页面组件执行任何操作,例如显示其名称:
Console.WriteLine(component.ToString());
//从这里您可以访问有关组件的大部分静态信息,
//但是如果你想获得页面标题或类似的信息,你必须创建
//一些自定义属性并用它们装饰页面。
}
}
} 

希望它有帮助

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

Sitemap.cs

使用Microsoft.AspNetCore.Components;
使用Microsoft.AspNetCore.Http;
使用System.Linq;
运用系统反思;
使用System.Threading.Tasks;
公共类站点地图{
公共静态异步任务生成(HttpContext上下文){
var pages=Assembly.getExecutionGassembly().ExportedTypes.Where(p=>
p、 IsSubclassOf(typeof(ComponentBase))&&p.Namespace==“your Namespace.Pages”);
foreach(页面中的变量页面){
//对页面组件执行任何操作,例如显示其名称:
wait context.Response.WriteAsync(page.FullName+“\n”);
//从这里,您可以访问有关
//组成部分,
//但是如果你想获得页面标题或类似的信息,你必须
//创造
//一些自定义属性并用它们装饰页面。
}
}
}
并更改startup.cs

app.UseEndpoints(端点=>{
endpoints.MapGet(“/sitemap.xml”),异步上下文=>{
等待站点地图。生成(上下文);
});
endpoints.MapBlazorHub();
endpoints.MapFallbackToPage(“/_主机”);
});

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

Sitemap.cs

使用Microsoft.AspNetCore.Components;
使用Microsoft.AspNetCore.Http;
使用System.Linq;
运用系统反思;
使用System.Threading.Tasks;
公共类站点地图{
公共静态异步任务生成(HttpContext上下文){
var pages=Assembly.getExecutionGassembly().ExportedTypes.Where(p=>
p、 IsSubclassOf(typeof(ComponentBase))&&p.Namespace==“your Namespace.Pages”);
foreach(页面中的变量页面){
//对页面组件执行任何操作,例如显示其名称:
wait context.Response.WriteAsync(page.FullName+“\n”);
//从这里,您可以访问有关
//组成部分,
//但是如果你想获得页面标题或类似的信息,你必须
//创造
//一些自定义属性并用它们装饰页面。
}
}
}
并更改startup.cs

app.UseEndpoints(端点=>{