Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Iterate ASP.NET MVC views to find all views supporting a specific model type

I would like to get a list of all views that support rendering a specific model type.

Pseudo code:

IEnumerable GetViewsByModelType(Type modelType)
{
   foreach (var view in SomeWayToGetAllViews())
   {
      if (typeof(view.ModelType).IsAssignableFrom(modelType))
      {
         yield return view; // This view supports the specified model type
      }
   }
}

In other words, given that I have a MyClass model, I'd like to find all views that would support rendering it. I.e. all views where the @model type is MyClass, or a type in its inheritance chain.

like image 346
Ted Nyberg Avatar asked Oct 31 '14 18:10

Ted Nyberg


People also ask

What are views in ASP NET MVC?

The Views in ASP.NET MVC application provides the separation of concerns (codes). It means, it separates the user interface from the rest of the application such as the business layer and data access layer. The ASP.NET MVC views use the advanced Razor syntax which makes it easy to switch between the HTML and C# code.

How to create a MVC view demo in Visual Studio?

ASP.NET MVC - Views 1 − Open the Visual Studio and click File → New → Project menu option. A new Project dialog opens. 2 − From the left pane, select Templates → Visual C# → Web. 3 − In the middle pane, select ASP.NET Web Application. 4 − Enter the project name ‘MVCViewDemo’ in the Name field and click Ok to continue. ... More items...

Where MVC view files are stored in MVC?

Where ASP.NET MVC View Files are Stored? In ASP.NET MVC, the views are having a “.cshtml” extension when we use C# as the programming language with Razor syntax. Usually, each controller will have its own folder in which the controller-specific .cshtml files (views) are going to be saved.

What are the benefits of using an AspView component?

View components provide the same SoC that controllers and views offer. They can eliminate the need for actions and views that deal with data used by common user interface elements. Like many other aspects of ASP.NET Core, views support dependency injection, allowing services to be injected into views.


1 Answers

Based on my findings the compiled views are not included in the assembly, so it's not going to be a walk in the park reflection.

In my opinion your best bet is going to be to list the .cshtml razor views and then use the BuildManager class to compile the type, which will allow you to get the Model property type.

Here is an example of looking for all Razor views that have a @Model type of LoginViewModel:

var dir = Directory.GetFiles(string.Format("{0}/Views", HostingEnvironment.ApplicationPhysicalPath), 
    "*.cshtml", SearchOption.AllDirectories);

foreach (var file in dir)
{
    var relativePath = file.Replace(HostingEnvironment.ApplicationPhysicalPath, String.Empty);

    Type type = BuildManager.GetCompiledType(relativePath);

    var modelProperty = type.GetProperties().FirstOrDefault(p => p.Name == "Model");

    if (modelProperty != null && modelProperty.PropertyType == typeof(LoginViewModel))
    {
        // You got the correct type
    }
}
like image 171
Faris Zacina Avatar answered Nov 15 '22 18:11

Faris Zacina