bizzehdee
bizzehdee

Reputation: 20993

List all views in ASP.MVC

Is it possible to list all views within a project with their relative path? Without having to statically type out all of the views manually.

I have looked around the Razor namespaces and classes and the ViewEngine namespaces and classes and cant see anything myself.

Upvotes: 1

Views: 2263

Answers (2)

Paras Parmar
Paras Parmar

Reputation: 343

This may be an old question, but here goes.

Option 1

You need to retrieve the assembly name from the Global.asax file. The namespace and the very first public class should be combined to get the assembly name below.

namespace XYZ
{
    public class MvcApplication : System.Web.HttpApplication

The assembly is XYZ.MvcApplication

The query below will retrieve a list of all controllers and action methods in an ASP.NET MVC project. The code below will help you enumerate it to a view.

    @using System.Reflection;
    @{
        ViewBag.Title = "Dashboard";

        Assembly asm = Assembly.GetAssembly(typeof(XYZ.MvcApplication));

    var paths = asm.GetTypes()
    .Where(type => typeof(System.Web.Mvc.Controller).IsAssignableFrom(type))
    .SelectMany(type => type.GetMethods(BindingFlags.Instance | BindingFlags.DeclaredOnly | BindingFlags.Public))
    .Where(m => !m.GetCustomAttributes(typeof(System.Runtime.CompilerServices.CompilerGeneratedAttribute), true).Any())
    .Select(x => new { Controller = x.DeclaringType.Name.Replace("Controller", null), Method = x.Name })
    .OrderBy(x => x.Controller).ThenBy(x => x.Method).ToList();
    }


                <table class="table table-bordered table-condensed table-striped">
                    <thead>
                        <tr>
                            <th>Controller</th>
                            <th>Method</th>
                        </tr>
                    </thead>
                    <tbody>
                        @foreach (var i in paths)
                        {
                            <tr>
                                <td>@i.Controller</td>
                                <td>@i.Method</td>
                            </tr>
                        }
                    </tbody>
                </table>

Option 2

There is another option for your non-tech colleagues to obtain a list of files used within your views.

Use a free software called Freeplane to get the kind of result below.

Free Plane >> Import Folder Structure

Please use the File >> Import >> Folder Structure Feature to get the specific Views you need.

Import Views Folder Structure

These can then be exported as an Excel file using File >> Export Map

Upvotes: 1

Sergey Shabanov
Sergey Shabanov

Reputation: 176

If you want just all list of your views just check the files under the ~/Views directory (Server.MapPath). I think there is no need of reflection.

Upvotes: 0

Related Questions