Jura Gorohovsky
Jura Gorohovsky

Reputation: 10148

In ASP.NET Core Razor Pages, how to get view engine path for a page outside that page's context?

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.

Upvotes: 0

Views: 1771

Answers (1)

Brad Patton
Brad Patton

Reputation: 4205

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>

Upvotes: 2

Related Questions