简体   繁体   English

Blazor 如何从 blazor 服务器端项目中的 razor 页面获取所有路由 url

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

I encountered such a problem.我遇到了这样的问题。 I need to get a list of all url-route from razor pages with value of attribute Authorize Such as我需要从具有属性 Authorize 值的剃刀页面获取所有 url-route 的列表,例如

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

I try to do it by EndpointsDataSource, but not getting the right result我尝试通过 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();        
    }
}

But I don't even get a list of route urls, not to mention the attribute value.但我什至没有得到路由 url 列表,更不用说属性值了。 Can anyone have any ideas how to do this?任何人都可以有任何想法如何做到这一点?

Replace the code in the Index component with the following code, and add this directive to the FetchData component: @attribute [Authorize]将 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;
  }

 } 

If you're using the default Blazor template, the output should be:如果您使用的是默认 Blazor 模板,则输出应为:

XXX.App XXX.App

XXX.Shared.MainLayout XXX.Shared.MainLayout

XXX.Shared.NavMenu XXX.Shared.NavMenu

XXX.Pages.Error XXX.Pages.Error

XXX.Pages.FetchData XXX.Pages.FetchData

Microsoft.AspNetCore.Authorization.AuthorizeAttribute Microsoft.AspNetCore.Authorization.AuthorizeAttribute

XXX.Pages.Index XXX.Pages.Index

Hope this helps...希望这可以帮助...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM