0

Say I have an Index page directly under Pages. In the context of that page, I can access PageContext.ActionDescriptor.ViewEnginePath that stores the page's path, and get /Index.

How do I get view engine path for any particular page outside a page's context? Does ASP.NET Core maintain a collection with view engine paths for all available pages/views that I can access?

This is an ASP.NET Core 2.2 application.

Jura Gorohovsky
  • 9,886
  • 40
  • 46
  • Based on the answer given, this is probably related to https://stackoverflow.com/questions/44455384/how-to-find-all-controller-and-action/44457348, https://stackoverflow.com/questions/41908957/get-all-registered-routes-in-asp-net-core – Jura Gorohovsky Jun 02 '19 at 21:59

1 Answers1

2

I have the following razor page that I use for debugging all of the route info. You can use as is or grab the _actionDescriptorCollectionProvider.ActionDescriptors.Items and look for the specific value you are looking for.

.cs code:

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; }
    }
}
}

With a cshtml page to view it nicely in a 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>
Brad Patton
  • 4,007
  • 4
  • 34
  • 44
  • Thanks! This looks like a step in the right direction for me. Unfortunately, `ActionDescriptors.Items` doesn't seem to contain any hint of types of page models. – Jura Gorohovsky Jun 02 '19 at 21:56
  • Ideally, I'd like to be able to find a view engine path given a page model type name. The idea is to have a simple repository to register pages in my application, define relationships between them, and use the repository to generate a table of contents, like this: `public static Article WhatsNew { get; set; } = new Article { Name = "What's New", Parent = Introduction, View = typeof(WhatsNewModel)};` – Jura Gorohovsky Jun 02 '19 at 21:57
  • Not sure how to proceed other than just stripping the "Model" substring and searching for a match in action descriptors, which feels ugly. I might be doing something weird though, as I'm not an experienced .NET Core dev at all. – Jura Gorohovsky Jun 02 '19 at 21:58