2
votes

I'm developing a library for use in ASP.NET Core MVC. For this library, I need to get a list of all (compiled) Razor views (path and content) that exist in the application.

Is there a way to lookup all compiled Razor views in the application during runtime? So far I have not had any luck trying to find out.

Iterating through .cshtml files is no option because they will not be published when using compiled views.

1
I think you could not get the content directly. For view content, it will use view data to create this view content. You may could not create the view without viewdata. - Edward

1 Answers

4
votes

For getting complied views path, you could try ViewsFeature like

public class HomeController : Controller
{
    private readonly IViewCompilerProvider _viewCompilerProvider;
    private readonly ApplicationPartManager _applicationPartManager;

    public HomeController(IViewCompilerProvider viewCompilerProvider
        , ApplicationPartManager applicationPartManager)
    {
        _viewCompilerProvider = viewCompilerProvider;
        _applicationPartManager = applicationPartManager;
    }
    public IActionResult Index()
    {
        var feature = new ViewsFeature();
        _applicationPartManager.PopulateFeature(feature);
        var views = feature.ViewDescriptors;          
        return View();
    }
}