在 ASP.NET 核心 Razor 页面中,如何获取页面上下文之外的页面的视图引擎路径?
In ASP.NET Core Razor Pages, how to get view engine path for a page outside that page's context?
假设我在 Pages
下有一个 Index
页面。在该页面的上下文中,我可以访问存储页面路径的 PageContext.ActionDescriptor.ViewEnginePath
,并获取 /Index
.
如何获取页面上下文之外的任何特定页面的视图引擎路径? ASP.NET Core 是否为我可以访问的所有可用 pages/views 维护一个包含视图引擎路径的集合?
这是一个 ASP.NET Core 2.2 应用程序。
我有以下用于调试所有路线信息的剃刀页面。您可以按原样使用或获取 _actionDescriptorCollectionProvider.ActionDescriptors.Items
并查找您要查找的特定值。
.cs代码:
using Microsoft.AspNetCore.Mvc.Infrastructure;
using Microsoft.AspNetCore.Mvc.RazorPages;
using Newtonsoft.Json;
using System.Collections.Generic;
using System.Linq;
namespace RouteDebugging.Pages {
public class RoutesModel : PageModel {
private readonly IActionDescriptorCollectionProvider _actionDescriptorCollectionProvider;
public RoutesModel(IActionDescriptorCollectionProvider actionDescriptorCollectionProvider) {
this._actionDescriptorCollectionProvider = actionDescriptorCollectionProvider;
}
public List<RouteInfo> Routes { get; set; }
public void OnGet() {
Routes = _actionDescriptorCollectionProvider.ActionDescriptors.Items
.Select(x => new RouteInfo {
Action = x.RouteValues["Action"],
Controller = x.RouteValues["Controller"],
Name = x.AttributeRouteInfo?.Name,
Template = x.AttributeRouteInfo?.Template,
Constraint = x.ActionConstraints == null ? "" : JsonConvert.SerializeObject(x.ActionConstraints)
})
.OrderBy(r => r.Template)
.ToList();
}
public class RouteInfo {
public string Template { get; set; }
public string Name { get; set; }
public string Controller { get; set; }
public string Action { get; set; }
public string Constraint { get; set; }
}
}
}
使用 cshtml 页面可以在 table 中很好地查看它:
@page
@model RouteDebugging.Pages.RoutesModel
@{
ViewData["Title"] = "Routes";
}
<h2>@ViewData["Title"]</h2>
<h3>Route Debug Info</h3>
<table class="table table-striped table-bordered">
<thead>
<tr>
<th>Route Template</th>
<th>Controller</th>
<th>Action</th>
<th>Constraints/Verbs</th>
<th>Name</th>
</tr>
</thead>
<tbody>
@foreach (var route in Model.Routes) {
@if (!String.IsNullOrEmpty(route.Template)) {
<tr>
<td>@route.Template</td>
<td>@route.Controller</td>
<td>@route.Action</td>
<td>@route.Constraint</td>
<td>@route.Name</td>
</tr>
}
}
</tbody>
</table>
假设我在 Pages
下有一个 Index
页面。在该页面的上下文中,我可以访问存储页面路径的 PageContext.ActionDescriptor.ViewEnginePath
,并获取 /Index
.
如何获取页面上下文之外的任何特定页面的视图引擎路径? ASP.NET Core 是否为我可以访问的所有可用 pages/views 维护一个包含视图引擎路径的集合?
这是一个 ASP.NET Core 2.2 应用程序。
我有以下用于调试所有路线信息的剃刀页面。您可以按原样使用或获取 _actionDescriptorCollectionProvider.ActionDescriptors.Items
并查找您要查找的特定值。
.cs代码:
using Microsoft.AspNetCore.Mvc.Infrastructure;
using Microsoft.AspNetCore.Mvc.RazorPages;
using Newtonsoft.Json;
using System.Collections.Generic;
using System.Linq;
namespace RouteDebugging.Pages {
public class RoutesModel : PageModel {
private readonly IActionDescriptorCollectionProvider _actionDescriptorCollectionProvider;
public RoutesModel(IActionDescriptorCollectionProvider actionDescriptorCollectionProvider) {
this._actionDescriptorCollectionProvider = actionDescriptorCollectionProvider;
}
public List<RouteInfo> Routes { get; set; }
public void OnGet() {
Routes = _actionDescriptorCollectionProvider.ActionDescriptors.Items
.Select(x => new RouteInfo {
Action = x.RouteValues["Action"],
Controller = x.RouteValues["Controller"],
Name = x.AttributeRouteInfo?.Name,
Template = x.AttributeRouteInfo?.Template,
Constraint = x.ActionConstraints == null ? "" : JsonConvert.SerializeObject(x.ActionConstraints)
})
.OrderBy(r => r.Template)
.ToList();
}
public class RouteInfo {
public string Template { get; set; }
public string Name { get; set; }
public string Controller { get; set; }
public string Action { get; set; }
public string Constraint { get; set; }
}
}
}
使用 cshtml 页面可以在 table 中很好地查看它:
@page
@model RouteDebugging.Pages.RoutesModel
@{
ViewData["Title"] = "Routes";
}
<h2>@ViewData["Title"]</h2>
<h3>Route Debug Info</h3>
<table class="table table-striped table-bordered">
<thead>
<tr>
<th>Route Template</th>
<th>Controller</th>
<th>Action</th>
<th>Constraints/Verbs</th>
<th>Name</th>
</tr>
</thead>
<tbody>
@foreach (var route in Model.Routes) {
@if (!String.IsNullOrEmpty(route.Template)) {
<tr>
<td>@route.Template</td>
<td>@route.Controller</td>
<td>@route.Action</td>
<td>@route.Constraint</td>
<td>@route.Name</td>
</tr>
}
}
</tbody>
</table>