【问题标题】:In ASP.NET Core Razor Pages, how to get view engine path for a page outside that page's context?在 ASP.NET Core Razor Pages 中,如何获取该页面上下文之外的页面的视图引擎路径?
【发布时间】:2019-05-31 21:24:55
【问题描述】:

假设我在Pages 下有一个Index 页面。在该页面的上下文中,我可以访问存储页面路径的PageContext.ActionDescriptor.ViewEnginePath,并获取/Index

如何获取页面上下文之外任何特定页面的视图引擎路径? ASP.NET Core 是否维护一个集合,其中包含我可以访问的所有可用页面/视图的视图引擎路径?

这是一个 ASP.NET Core 2.2 应用程序。

【问题讨论】:

标签: asp.net asp.net-core razor-pages


【解决方案1】:

我有以下用于调试所有路线信息的剃须刀页面。您可以按原样使用或获取_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页面在表格中很好地查看它:

@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>

【讨论】:

  • 谢谢!对我来说,这看起来是朝着正确方向迈出的一步。不幸的是,ActionDescriptors.Items 似乎没有包含任何页面模型类型的提示。
  • 理想情况下,我希望能够在给定页面模型类型名称的情况下找到视图引擎路径。这个想法是有一个简单的存储库来在我的应用程序中注册页面,定义它们之间的关系,并使用存储库生成目录,如下所示:public static Article WhatsNew { get; set; } = new Article { Name = "What's New", Parent = Introduction, View = typeof(WhatsNewModel)};
  • 除了剥离“模型”子字符串并在动作描述符中搜索匹配项之外,不知道如何继续,这感觉很难看。不过,我可能会做一些奇怪的事情,因为我根本不是经验丰富的 .NET Core 开发人员。
猜你喜欢
  • 2018-11-16
  • 2018-02-24
  • 2018-04-05
  • 2019-05-22
  • 2020-05-06
  • 2022-01-19
  • 2020-11-29
  • 2018-06-05
  • 2021-09-28
相关资源
最近更新 更多