Simple command-line HTTPS server for the .NET Core CLI
A simple command-line HTTP server.
It launches a server in the current working directory and serves all files in it.
Install .NET 5 or newer and run this command:
dotnet tool install --global dotnet-serve
Start a simple server and open the browser by running
dotnet serve -o
..and with HTTPS.
dotnet serve -o -S
Usage: dotnet serve [options]
Options: --version Show version information -d|--directory
The root directory to serve. [Current directory] -o|--open-browser Open a web browser when the server starts. [false] -p|--port Port to use . Use 0 for a dynamic port. -a|--address Address to use [127.0.0.1] --path-base The base URL path of postpended to the site url. --default-extensions[:] A comma-delimited list of extensions to use when no extension is provided in the URL. [.html,.htm] -q|--quiet Show less console output. -v|--verbose Show more console output. -h|--headers A header to return with all file/directory responses. e.g. -h "X-XSS-Protection: 1; mode=block" -S|--tls Enable TLS (HTTPS) --cert A PEM encoded certificate file to use for HTTPS connections. Defaults to file in current directory named 'cert.pem' --key A PEM encoded private key to use for HTTPS connections. Defaults to file in current directory named 'private.key' --pfx A PKCS#12 certificate file to use for HTTPS connections. Defaults to file in current directory named 'cert.pfx' --pfx-pwd The password to open the certificate file. (Optional) -m|--mime Add a mapping from file extension to MIME type. Empty MIME removes a mapping. Expected format is =. -z|--gzip Enable gzip compression -b|--brotli Enable brotli compression -c|--cors Enable CORS (It will enable CORS for all origin and all methods) --save-options Save specified options to .netconfig for subsequent runs. -?|--help Show help information
Tip: single letters for options can be combined. Example:dotnet serve -Sozq
dotnet serve -Swill serve requests over HTTPS. By default, it will attempt to find an appropriate certificate on the machine.
dotnet servewill look for, in order: - A pair of files named
private.keyin the current directory - A file named
cert.pfxin the current directory - The ASP.NET Core Developer Certificate (localhost only)
You can also manually specify certificates as command line options (see below):
See also this doc for how to create a self-signed HTTPS certificate.
Use this when you have your certficate and private key stored in separate files (PEM encoded).
dotnet serve --cert ./cert.pem --key ./private.pem
Note: currently only RSA private keys are supported.
You can generate a self-signed
Use this when you have your certficate as a .pfx/.p12 file (PKCS#12 format).
dotnet serve --pfx myCert.pfx --pfx-pwd certPass123
The developer certificate is automatically created the first time you use
dotnet. When serving on 'localhost', dotnet-serve will discover and use when you run:
dotnet serve -S
dotnet-servesupports reading and saving options using dotnet-config, which provides hierarchical inherited configuration for any .NET tool. This means you can save your frequently used options to
.netconfigso you don't need to specify them every time and for every folder you serve across your machine.
To save the options used in a particular run to the current directory's
.netconfig, just append
dotnet serve -p 8080 --gzip --cors --quiet --save-options
After running that command, a new
.netconfigwill be created (if there isn't one already there) with the following section for
[serve] port = 8000 quiet gzip cors header = X-My-Option: foo header = X-Another: bar
mimetype mappings and
exclude-fileentries can be provided as individual variables)
You can place those settings in any parent folder and it will be reused across all descendent folders, or they can also be saved to the global (user profile) or system locations. To easily configure these options at those levels, use the
dotnet config --global --set serve.port 8000
This will default the port to
8000whenever a port is not specified in the command line. You can open the saved
pfxvalues, in particular, can be relative paths that are resolved relative to the location of the declaring
.netconfigfile, which can be very convenient.