How to store code snippets in Visual Studio for re-use later

Storing code snippets in the Visual Studio Toolbox is really useful when we are preparing for a demo or tutorial. We can add snippets beforehand and then during our demo we can just double click or drag them onto the file we are editing.

Save snippets into the Toolbox

First, open the toolbox with the CTRL+ALT+X shortcut or from the View -> Toolbox menu item.

To keep things organised, right click on the toolbox and click ‘Add Tab’ and enter an appropriate name depending on what items you want to store in the tab.

To add code to your tab simply select the code block and drag it across into your tab.

In the below example I’ve added a tab called ‘ENTITY FRAMEWORK DEMO’ and have added two code snippets…

Visual Studio Code Snippets

If we right click on a snippet we have the option to rename it to something more helpful…

Code snippets can be renamed


How to launch a web app in multiple browsers from Visual Studio at the same time

In Visual Studio to launch your web app in multiple browsers simply set multiple defaults in the ‘Browse With…’ screen as shown below. For this to work you’ll need to start without debugging (CTRL+F5).

This is helpful when you want to do a cross-browser check.

How to launch app in multiple browsers in Visual Studio

How to scaffold multiple ASP.NET CORE MVC controllers at once using a bat file

Previously I wrote about how we can customise the templates Visual Studio uses when it scaffolds a controller.

After changing a template we can re-scaffold our controllers one by one from the GUI provided by Visual Studio…

Scaffold a controller in Visual Studio

… BUT this can be very time consuming if we want to re-scaffold a lot of controllers each time we make a change to any of the controller or view templates.

A better way is to trigger the scaffolding process for multiple controllers at once by wrapping calls to the core scaffolding engine in a bat file.

In the example below (click for a larger view in a new window) I’m scaffolding three different controllers…

Scaffolding via the command lineI’ve included a call to install the aspnet-codegenerator global tool which we need to trigger scaffolding at the top of the file. You don’t have to include this, but I like to keep it there just to make things easier for the next developer.

The command that I’ve used to scaffold the CustomersController is…

dotnet aspnet-codegenerator controller – controllerName CustomersController – model Customer – dataContext EFContext – relativeFolderPath Controllers – force – useDefaultLayout – referenceScriptLibraries – layout "~/Views/Shared/_Layout.cshtml"

The flags I’ve set match the defaults which are set when we create a scaffolded controller through the GUI. Note the inclusion of the force flag which when included means existing files will be overwritten.

Since I’ve set the relativeFolderPath flag to ‘Controllers’ this means I need to save the .bat file in the root of my project.

If you’re copying and pasting from above, note WordPress has messed up the dashes. Each flag should have two dashes in front of it… see the image example above.

Running the scaffolding bat file from within Visual Studio

We can run the bat file by double clicking it in the file system but to run it from within Visual Studio we can install the Open Command Line extension. After installing the extension we can right click the .bat file and select ‘Execute File’…

Execute File

and we’ll see the CMD window pop up with output similar to below…controller-scaffold-batfile

How to run Entity Framework Core DB scaffolding from a .bat file in Visual Studio

It’s very handy to wrap the Entity Framework Scaffolding command in a bat file and run it on demand from within Visual Studio. Thankfully this is easy to do…

Installing required dependencies

Open up a command prompt window, navigate to your EF project folder and run the below commands to make sure you have the required dependencies…

dotnet tool install – global dotnet-ef
dotnet add package Microsoft.EntityFrameworkCore.Tools

There’s a good chance you’ll already have these installed.

Create a .bat file which calls the ef dbcontext scaffold command

Put a command similar to below in a .bat file and save it in whatever project your EF DbContext and Models are.

dotnet ef dbcontext scaffold name="AdventureWorks" Microsoft.EntityFrameworkCore.SqlServer – force – output-dir Models – context-dir Context – context EFContext – use-database-names

In the above example we have a named connection which references a connection in the appsettings.json file.

To understand what all the various parameters and flags mean visit the EF Core tools reference (.NET CLI) – EF Core page.

If you’re copying and pasting from above, note WordPress has messed up the dashes. Each flag should have two dashes in front of it.

Executing the .bat file in Visual Studio

At this point you can just double click the file in the project folder and it will scaffold.

To run it from within Visual Studio we can install the Open Command Line extension. After installing the extension we can right click the .bat file and select ‘Execute File’…

Execute File

and we’ll see the CMD window popup…

CMD popup

‘∩╗┐dotnet’ is not recognized as an internal or external command

If you get this strange looking error when trying to run the .bat file it is probably saved in UTF-8 encoding format which cmd.exe doesn’t support. To resolve open the bat file in Notepad and save it with ANSI encoding

Save as ANSI encoding

Override the default controller scaffold templates in Visual Studio 2019 and .NET 5.0

The real power of scaffolding in Visual Studio comes from the ability to override the templates which are used to generate the controllers and views. This gives us complete control of the C# and HTML which is emitted by the scaffolding process.

The templates for Visual Studio 2019 and .NET 5.0 are stored in C:\Users\USERNAME\.nuget\packages\microsoft.visualstudio.web.codegenerators.mvc\5.0.2 as shown below…

Where to find scaffold templates

We can update these templates directly in the above location but this is problematic as it is local to a developers machine and therefore not easily shared with other team members and because changes made here would affect all projects on the machine which may not be desired.

A better way is to copy the templates into our MVC projects like below so they can be checked into source control and shared across the team.

Required folder structure for custom scaffolding

This works as by convention Visual Studio will look for a Templates folder in the MVC project first before using the machine level folder.

Note… you may see build errors related to the templates you’ve just copied into your project before you first try to scaffold. These will go away when Microsoft.VisualStudio.Web.CodeGeneration.Design is installed which Visual Studio will do as part of the scaffold process.

Scaffolding multiple controllers at once from the command line

After you’ve customised your templates you can rescaffold your controllers one by one manually from the GUI, but a better way is to call the core scaffolding engine from a bat file.