Blazor 如何从 Blazor 服务器端项目中的剃刀页面获取所有路由 URL

Blazor How Get all route urls from razor pages in blazor server side project

我遇到过这样的问题。我需要从具有属性授权值的剃刀页面获取所有 url-route 的列表 如

@page "/counter" 
@attribute [Authorize("IsAdmin")]

我尝试通过 EndpointsDataSource 来完成,但没有得到正确的结果

@using Microsoft.AspNetCore.Routing 
@using Microsoft.AspNetCore.Mvc.ApplicationModels 
@using Microsoft.AspNetCore.Mvc.RazorPages 
@using Microsoft.AspNetCore.Http 
@using Microsoft.Extensions.DependencyInjection 
@using Microsoft.AspNetCore.Mvc.Routing 
@inject EndpointDataSource EndpointsDataSource

<table class="table">
    <thead class="thead-dark">
        <tr>
            <th scope="col">Order</th>
            <th scope="col">Display Name</th>
            <th scope="col">Route Pattern</th>
            <th scope="col">Metadata</th>
        </tr>
    </thead>
    <tbody>
        @foreach (var endpoint in Endpoints)
        {
            var routeEndpoint = endpoint as RouteEndpoint;
            <tr>
                <td>@routeEndpoint?.Order</td>
                <td>@endpoint.DisplayName</td>
                <td>@routeEndpoint?.RoutePattern.RawText</td>
                <td>
                    <ul>
                        @foreach (var md in endpoint.Metadata)
                        {
                            switch (md)
                            {                               
                                case PageRouteMetadata prm:
                                    <li>
                                        <p>@nameof(PageRouteMetadata)</p>
                                        <ul>
                                            <li>Page Route: @prm.PageRoute</li>
                                            <li>Route Template: @prm.RouteTemplate</li>
                                        </ul>
                                    </li>
                                    break;
                                case PageActionDescriptor pad:
                                    <li>
                                        <p>@nameof(PageActionDescriptor)</p>
                                        <ul>
                                            <li>Id: @pad.Id</li>
                                            <li>Area: @pad.AreaName</li>
                                            <li>Display Name: @pad.DisplayName</li>
                                            <li>View Engine Path: @pad.ViewEnginePath</li>
                                            <li>Relative Path: @pad.RelativePath</li>
                                        </ul>
                                    </li>
                                    break;
                                case RouteNameMetadata rnm:
                                    <li>
                                        Route Name Metadata: @rnm.RouteName
                                    </li>
                                    break;
                                case SuppressLinkGenerationMetadata slg:
                                    <li>
                                        suppress link: @slg.SuppressLinkGeneration;
                                    </li>
                                    break;
                                default:
                                    <li>@md.ToString()</li>
                                    break;
                            }
                        }
                    </ul>
                </td>
            </tr>
        }
    </tbody>
</table>    

@code{   
    public List<Microsoft.AspNetCore.Http.Endpoint> Endpoints { get; set; }

    protected override async Task OnInitializedAsync()
    {
        Endpoints = EndpointsDataSource.Endpoints.ToList();        
    }
}

但是我连路由列表url都没有,更不用说属性值了。谁能知道如何做到这一点?

将 Index 组件中的代码替换为以下代码,并将此指令添加到 FetchData 组件中:@attribute [Authorize]

 @page "/"
 @using System.Linq;
 @using System.Reflection;


 <button type="button" @onclick="@GetRouteUrlWithAuthorizeAttribute">Get Route 
     Url With Authorize Attribute</button>

 @code{

      private Task GetRouteUrlWithAuthorizeAttribute()
      {

          // Get all the components whose base class is ComponentBase
          var components = Assembly.GetExecutingAssembly()
                                 .ExportedTypes
                                 .Where(t => 
                                t.IsSubclassOf(typeof(ComponentBase)));

          foreach (var component in components)
          {
              // Print the name (Type) of the Component
              Console.WriteLine(component.ToString());

             // Now check if this component contains the Authorize attribute
             var allAttributes = component.GetCustomAttributes(inherit: true);

             var authorizeDataAttributes = 
                             allAttributes.OfType<IAuthorizeData>().ToArray();

             // If it does, show this to us... 
             foreach (var authorizeData in authorizeDataAttributes)
             {

                Console.WriteLine(authorizeData.ToString());
             }
        }

    return Task.CompletedTask;
  }

 } 

如果您使用的是默认的 Blazor 模板,输出应该是:

XXX.App

XXX.Shared.MainLayout

XXX.Shared.NavMenu

XXX.Pages.Error

XXX.Pages.FetchData

Microsoft.AspNetCore.Authorization.AuthorizeAttribute

XXX.Pages.Index

希望这对您有所帮助...