Scaffold .NET 8 MVC or API Controller

How to Scaffold .NET 8 MVC Controller and Views

Steps to Scaffold in .NET 8 Using .NET CLI

1: Install NuGet Packages
You will probably need to add the following NuGet packages to the Project you are doing the scaffolding from:

Microsoft.VisualStudio.Web.CodeGeneration.Design
Microsoft.EntityFrameworkCore.SqlServer
Microsoft.EntityFrameworkCore.Tools

If there is a problem scaffolding then you may need to Install the Code Generation Tool Globally to do this make sure the code generator tool (dotnet-aspnet-codegenerator) is installed globally. Run this in your terminal:


TIP: If the dotnet aspnet-codegenerator command (above or below) doesnt work, Save All and then restart visual studio.
 

Navigate to Your MVC Project Directory In your Terminal (Command Prompt, PowerShell), navigate to the directory where your Site.csproj file is located.

For example:

As mentioned earlier, adjust the -dc path to your DbContext as required.

Here’s what each argument means:

  • -name ContactsController: The name of the controller to be generated.
  • -m Contact: The model class (Contact) for which CRUD operations will be scaffolded.
  • -dc Data.ApplicationDbContext: The DbContext class (ApplicationDbContext) in your Data project. Make sure the namespace (Data) matches where your ApplicationDbContext is located.
  • --relativeFolderPath Controllers: This specifies where to generate the controller. It will be placed in the Controllers folder.
  • --useDefaultLayout: This ensures that the views generated will use the default layout (i.e., _Layout.cshtml).
  • --referenceScriptLibraries: This includes script libraries (e.g., for validation) in the generated views.

Check the Results After running this command:

  • A ContactsController.cs file should be generated in the Controllers folder of your MVC project.
  • Corresponding views (for Create, Edit, Delete, Details, and Index) will be generated in a Views/Contacts folder.

Additional Considerations

  • DbContext Location: Since your ApplicationDbContext is in the Data project, make sure the Data project is successfully referenced in your MVC project. This is confirmed by the <ProjectReference> tag in your Site.csproj, so that part looks good.

Optional additional Step

Add the following navigation links to your shared/_layout.cshtml file.

                        <li class="nav-item dropdown">
                            <a class="nav-link dropdown-toggle" href="#" id="contactsDropdown" role="button" data-bs-toggle="dropdown" aria-expanded="false">
                                Contacts
                            </a>
                            <ul class="dropdown-menu" aria-labelledby="contactsDropdown">
                                <li><a class="dropdown-item" asp-controller="Contacts" asp-action="Index">List Contacts</a></li>
                                <li><a class="dropdown-item" asp-controller="Contacts" asp-action="Create">Create Contact</a></li>
                            </ul>
                        </li>

How to Scaffold .NET 8 API Controller

Lets assume that our project looks like this when completed

 1: Dependency Injection setup:

: In the client program.cs we need to configure the HttpClient for dependency injection:

services.AddControllersWithViews(); 
services.AddHttpClient(); // Registers HttpClient
var app = builder.Build();