我想通过迭代如下可路由的Razor组件名称来以编程方式生成菜单列表。GetAllRoutableRazorComponentNames()
下面如何实现?可能吗?
@foreach (var filename in GetAllRoutableRazorComponentNames())
{
<li class="nav-item px-3">
<NavLink class="nav-link" href="@filename">
<span class="oi oi-list-rich" aria-hidden="true">@filename</span>
</NavLink>
</li>
}
好的,这仅返回可路由的组件。有时间的话,我也会改善它。但这确实满足您的要求:
@using System.Linq;
@using System.Reflection;
<button type="button" @onclick="GetRoutables">
Get Route
Url With Authorize Attribute
</button>
@code{
public void GetRoutables()
{
// Get all the components whose base class is ComponentBase
var components = Assembly.GetExecutingAssembly()
.ExportedTypes
.Where(t => t.IsSubclassOf(typeof(ComponentBase))).ToList();
var routables = components.Where(c =>
c.GetCustomAttributes(inherit: true).OfType<RouteAttribute>
().ToArray().Count() > 0);
foreach (var routable in routables)
{
Console.WriteLine(routable.ToString());
}
}
}
希望这可以帮助...
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句