【发布时间】:2020-02-24 13:06:54
【问题描述】:
我遇到了这样的问题。我需要从属性为 Authorize 的剃须刀页面获取所有 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 的列表,更不用说属性值了。谁能知道如何做到这一点?
【问题讨论】:
-
你知道这个自己的系统吗?它是如何使用的?
标签: c# asp.net-core .net-core blazor blazor-server-side