在ASP.NET Core Razor页面中,如何获取该页面以外页面的视图引擎路径';什么是背景?
假设我在在ASP.NET Core Razor页面中,如何获取该页面以外页面的视图引擎路径';什么是背景?,asp.net,asp.net-core,razor-pages,Asp.net,Asp.net Core,Razor Pages,假设我在页面的正下方有一个索引页面。在该页面的上下文中,我可以访问存储页面路径的PageContext.ActionDescriptor.ViewEnginePath,并获取/Index 如何获取页面上下文之外的任何特定页面的视图引擎路径?ASP.NET Core是否为我可以访问的所有可用页面/视图维护具有视图引擎路径的集合 这是一个ASP.NET Core 2.2应用程序。我有下面的razor页面,用于调试所有路由信息。您可以按原样使用或获取\u actionDescriptorCollect
页面
的正下方有一个索引
页面。在该页面的上下文中,我可以访问存储页面路径的PageContext.ActionDescriptor.ViewEnginePath
,并获取/Index
如何获取页面上下文之外的任何特定页面的视图引擎路径?ASP.NET Core是否为我可以访问的所有可用页面/视图维护具有视图引擎路径的集合
这是一个ASP.NET Core 2.2应用程序。我有下面的razor页面,用于调试所有路由信息。您可以按原样使用或获取
\u 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; }
}
}
}
使用Microsoft.AspNetCore.Mvc.Infrastructure;
使用Microsoft.AspNetCore.Mvc.RazorPages;
使用Newtonsoft.Json;
使用System.Collections.Generic;
使用System.Linq;
命名空间RoutedBugging.Pages{
公共类路由模型:PageModel{
专用只读IActionDescriptorCollectionProvider _actionDescriptorCollectionProvider;
公共路由模型(IActionDescriptorCollectionProvider操作描述符CollectionProvider){
这是._actionDescriptorCollectionProvider=actionDescriptorCollectionProvider;
}
公共列表路由{get;set;}
公共互联网{
Routes=\u actionDescriptorCollectionProvider.ActionDescriptors.Items
.选择(x=>new RouteInfo{
Action=x.RouteValues[“Action”],
控制器=x.RouteValue[“控制器”],
Name=x.AttributeRouteInfo?.Name,
Template=x.AttributeRouteInfo?.Template,
Constraint=x.ActionConstraints==null?”:JsonConvert.SerializeObject(x.ActionConstraints)
})
.OrderBy(r=>r.Template)
.ToList();
}
公共类路由信息{
公共字符串模板{get;set;}
公共字符串名称{get;set;}
公共字符串控制器{get;set;}
公共字符串操作{get;set;}
公共字符串约束{get;set;}
}
}
}
使用cshtml页面在表中很好地查看:
@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>
@page
@模型RoutedBugging.Pages.RouteModel
@{
ViewData[“标题”]=“路线”;
}
@ViewData[“标题”]
路由调试信息
路由模板
控制器
行动
约束/动词
名称
@foreach(Model.Routes中的var路由){
@如果(!String.IsNullOrEmpty(route.Template)){
@路由模板
@路线控制员
@路线,行动
@路径约束
@路线。名称
}
}
我有下面的razor页面,用于调试所有路由信息。您可以按原样使用或获取\u 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; }
}
}
}
使用Microsoft.AspNetCore.Mvc.Infrastructure;
使用Microsoft.AspNetCore.Mvc.RazorPages;
使用Newtonsoft.Json;
使用System.Collections.Generic;
使用System.Linq;
命名空间RoutedBugging.Pages{
公共类路由模型:PageModel{
专用只读IActionDescriptorCollectionProvider _actionDescriptorCollectionProvider;
公共路由模型(IActionDescriptorCollectionProvider操作描述符CollectionProvider){
这是._actionDescriptorCollectionProvider=actionDescriptorCollectionProvider;
}
公共列表路由{get;set;}
公共互联网{
Routes=\u actionDescriptorCollectionProvider.ActionDescriptors.Items
.选择(x=>new RouteInfo{
Action=x.RouteValues[“Action”],
控制器=x.RouteValue[“控制器”],
Name=x.AttributeRouteInfo?.Name,
Template=x.AttributeRouteInfo?.Template,
Constraint=x.ActionConstraints==null?”:JsonConvert.SerializeObject(x.ActionConstraints)
})
.OrderBy(r=>r.Template)
.ToList();
}
公共类路由信息{
公共字符串模板{get;set;}
公共字符串名称{get;set;}
公共字符串控制器{get;set;}
公共字符串操作{get;set;}
公共字符串约束{get;set;}
}
}
}
使用cshtml页面在表中很好地查看:
@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>
@page
@模型RoutedBugging.Pages.RouteModel
@{
ViewData[“标题”]=“路线”;
}
@ViewData[“标题”]
路由调试信息
路由模板
控制器
行动
约束/动词
名称
@foreach(Model.Routes中的var路由){
@如果(!String.IsNullOrEmpty(route.Template)){
@路由模板
@路线控制员
@路线,行动
@路径约束
@路线。名称
}
}
谢谢!对我来说,这似乎是朝着正确方向迈出的一步。不幸的是,actionDescriptor.Items
似乎不包含任何页面模型类型的提示。理想情况下,我希望能够找到给定页面模型类型名称的视图引擎路径。其思想是使用一个简单的存储库在我的应用程序中注册页面,定义页面之间的关系,并使用存储库生成一个目录,如下所示:public static Article WhatsNew{get;set;}=newarticle{Name=“What's new”,Parent=Introduction,View=typeof(WhatsNewModel)}代码>不确定如何继续,而只是剥离“Model”子字符串并在中搜索匹配项