Creating Views and Controllers in a .NET Core App With ASP.NET Code Generator

Created By

Mario Mendieta

July 15, 2019

Getting Started

  • In our previous tutorial we reverse engineered a Movies database to generate an EF Model that maps entities in our web app to tables in our database. We did this using simple CLI tools. In this tutorial we will use the same technique to generate our custom views and controllers. We will use a tool called ASP.NET Code Generator (dotnet-aspnet-codegenerator) that allows us to do just that.

    To begin, make sure you have the scaffolding tool installed. To install the latest stable version, execute the following command:

    dotnet tool install -g dotnet-aspnet-codegenerator
    This will globaly install the aspnet-codegenerator tool that we will use throughout this tutorial. You will only need to do this once per machine.

Code Generation

  • We will first download a required NuGet package into our .NET Core application, and then proceed to generate our views and controller. To follow along you would need to have a .NET Core app already created. To view how to create a new .NET Core app using CLI, follow steps 1 to 3 in our previous tutorial

    1. Navigate to your project’s directory (where your .csproj file is located):

      cd EFCoreScaffoldWebApp

    2. Add the required CodeGeneration NuGet package:

      dotnet add package Microsoft.VisualStudio.Web.CodeGeneration.Design
      This package contains the scaffolding tool: dotnet-aspnet-codegenerator. Although we will use this tool to generate our views and controller, it features a variety of different generators for producing MVC areas, Razor pages, and identity. We will explore these generators in different tutorials.

    3. Build the project:

      dotnet build

    4. We are now ready to start using the aspnet-codegenerator tool. To create the controller and views for a specific entity, run the following command:

      dotnet aspnet-cogenerator controller -name [ENTITY]Controller -m [ENTITY] -dc [YOUR-DB-CONTEXT] —outDir Controllers --useDefaultLayout --referenceScriptLibraries
      This will generate a new controller with the specified parameters. First, we specify the controller name by using the -name option. In this example, our controller will take the name of our entity plus the word “Controller”, e.g. MovieGenreController.

      Next, we specify the actual entity that will be used to create our controller. We do this by using the -m (or --model) option, e.g. MovieGenre.

      We continue by specifying the database context that contains our EF model by using the -dc option, e.g. MoviesDbContext.

      We then specify the folder for our new controller by the using the -outDir option, e.g. the Controllers folder. If not specified, the controller will be created in the project’s root folder.

      We add the optional parameter --referenceScriptLibraries (or -scripts) to add _ValidationScriptsPartial to Edit and Create pages.

      For example, to generate the controller and views for the MovieGenre table in our Movies database, we run the following command:

      dotnet aspnet-codegenerator controller -name MovieGenreController -m MovieGenre -dc MoviesDbContext -outDir Controllers --useDefaultLayout --referenceScriptLibraries
      At this point, you will see that a new controller was added to the Controllers folder, and there is also a new folder with Create, Delete, Details, Edit, and Index pages within your Views folder.

  • If you would like to create a controller to be consumed by a web API that is not bound to a specific entity. Then add the -actions option to generate read and write actions, and the -api option to generate a controller with REST style API and no views:
    dotnet aspnet-codegenerator controller -name [CUSTOM-NAME]Controller -actions -api —outDir Controllers

Summary

  • In this tutorial we learned how to automatically generate Controllers and Views from entities within a .NET Core application. We used a single CLI tool called .NET Code Generator to allow us to create a user interface to manage our data. This is an useful feature to quickly build a CRUD interface in an MVC design pattern to manage data in a .NET Core application.

Get in Touch

Tell us about your project. We will get back to you as soon as possible.