English
Français

Blog of Denis VOITURON

for a better .NET world

.NET CLI to deploy a website locally

Posted on 2025-06-15

Overview

If, like me, you sometimes develop internal tools for your team of developers,you may be interested in learning how to create a dotnet tool package “manually”.

This article explains how to do this step by step.

Of course, for Console projects, you can add the <PackAsTool> and <ToolCommandName> parameters to your csproj. But for a website, you need a whole series of files that are only generated during publication.

1. Create your website

First, you need to have a website. You can quickly create one using dotnet new blazorserver.

Later, we will publish this website using dotnet publish -c Release -o ./bin/Publish so that the website will be available in a subfolder called bin/publish.

2. Set up your project

You must add these two files to configure your project as a DotNet Tool package.

3. Adapt your Program.cs

By default, your project should start with port 5000 with an Assets folder wwwroot referenced from the current folder. You can adapt this via this part of code to be placed in your Program class.

You can also automatically start the default browser to immediately display your website.

public class Program
{
	public static void Main(string[] args)
	{
#if (RELEASE)
		var wwwRootDirectory = Path.Combine(AppContext.BaseDirectory, "wwwroot");
		var options = new WebApplicationOptions
		{
			WebRootPath = wwwRootDirectory,
			Args = args,
		};

		var builder = WebApplication.CreateBuilder(options);

		builder.WebHost.ConfigureKestrel(serverOptions =>
		{
			serverOptions.ListenAnyIP(6789, listenOptions =>  // 👈 Set a specific port number
			{
            // Uses the trusted dev cert automatically
				listenOptions.UseHttps(); 
			});
		});
#else
		var builder = WebApplication.CreateBuilder(args);
#endif

		// Keep the existing code
      // ...

		var app = builder.Build();

		// Keep the existing code
      // ...

		// Launch browser only in release mode and when running locally
#if RELEASE
		if (OperatingSystem.IsWindows() || OperatingSystem.IsLinux() || OperatingSystem.IsMacOS())
		{
			var url = "https://localhost:6789"; // or your configured URL
                try
			{
				System.Diagnostics.Process.Start(new System.Diagnostics.ProcessStartInfo
				{
					FileName = url,
					UseShellExecute = true
				});
			}
			catch (Exception ex)
			{
				Console.WriteLine($"Failed to launch browser: {ex.Message}");
			}
		}
#endif

		app.Run();
	}
}

4. Create the DotNet Tool package

5. Install the tool before publishing it

Languages

EnglishEnglish
FrenchFrançais

Follow me

Recent posts