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



我遇到了这样一个问题。我需要从属性为Authorize的剃刀页面获得所有url路由的列表如

@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、 应用

XXX、 共享主布局

XXX、 共享导航菜单

XXX、 Pages.Error

XXX、 Pages.FetchData

Microsoft.AspNetCore.Authorization。AuthorizeAttribute

XXX、 Pages.索引

希望这能帮助。。。

相关内容

  • 没有找到相关文章

最新更新