Source Code

How to Publish ASP.NET Core Applications in Visual Studio Code

Learn how to publish and deploy ASP.NET Core apps using VS Code and .NET CLI. Follow our step-by-step guide to prepare your web application for production.

Steps to Publish an ASP.NET Core Application using Visual Studio Code

Method 1: The Universal CLI Approach (To Local Folder)

1. Ensure Prerequisites Are Met
   - Install [.NET SDK](https://dotnet.microsoft.com/download) if you haven't already.
   - Install Visual Studio Code with the [C# extension](https://marketplace.visualstudio.com/items?itemName=ms-dotnettools.csharp).

2. Open Your Project in Visual Studio Code
   - Open the folder containing your ASP.NET Core project in Visual Studio Code.

3. Build the Project
   - Open the integrated terminal in Visual Studio Code by pressing `Ctrl + ` (backtick) or navigating to Terminal > New Terminal from the menu.
   - Run the following command to build your project:

dotnet build

4. Publish the Application
   - In the terminal, run the following command to publish your application:

dotnet publish -c Release -o ./publish   

     - `-c Release`: Specifies that you're publishing the release version (you can also use `Debug`).
     - `-o ./publish`: Specifies the output directory for the published files. You can choose a different path if needed.

5. Locate the Published Files
   - After the command completes, you will find the published files in the output directory you specified (in this case, `./publish`).

6. Deploy the Application
   - Copy the contents of the `./publish` directory to your hosting environment (e.g., server, cloud provider, or container).
   - Depending on your hosting environment, you might need to set up an IIS server, a reverse proxy (like Nginx or Apache), or a container runtime (like Docker).

7. Run the Application
   - You can test running your published application locally by navigating to the published directory and using the following command:

dotnet YourAppName.dll  

   - Replace `YourAppName.dll` with the actual name of your application's DLL.
Example
Here's an example using these commands:

# Build the application
dotnet build
# Publish the application to a folder named "publish"
dotnet publish -c Release -o ./publish
# Run the published application locally (optional)
cd publish
dotnet YourAppName.dll

Optional: Publish for Specific Platforms
If you need to publish the application for a specific platform, you can add the `-r` option to specify the runtime identifier (RID). For example:

dotnet publish -c Release -r win10-x64 -o ./publish

Some common RIDs include:
- win10-x64 (Windows 64-bit)
- winux-x64 (Linux 64-bit)
- osx-x64 (macOS 64-bit)

Method 2: Publishing to Azure (Using Extensions)

If you are deploying to the cloud, the Azure extension makes this a "right-click" process.

  1. Generate the Publish Folder: Run the CLI command from Method 1 first.

  2. Open the Azure Side Bar: Click the Azure icon in the Activity Bar on the left.

  3. Sign In: Log into your Azure account.

  4. Deploy:

    • In the Explorer view, right-click your bin/Release/netX.X/publish folder.

    • Select Deploy to Web App...

    • Follow the prompts in the Command Palette to select your subscription and your Web App name.

 

Thanks for visit my website