How can you publish your web app or API from Visual Studio?

Here's how you can publish your web app or API from Visual Studio.

Prepare for Deployment

Initiate the Publish Process

Choose a Publish Target

Configure Publish Profile

Preview and Publish

Publish profile files in Visual Studio

A Visual Studio publishes a profile file with the extension. pubxml stores configurations for deploying your application. It essentially automates the publishing process by remembering various settings.

Please check the below thoughts for publishing profile files.

Benefits of Publish Profiles

Location of Publish Profile Files

Typically stored within your project directory in a folder called "Properties\PublishProfiles".

Content of Publish Profile Files

Written in XML and based on MSBuild, a build automation tool in Visual Studio.

Specify details like

Creating and Using Publish Profiles

Create File

Create File

Choose the publishing category.

Publish category

Specify the path of publishing files to place.

Publish­

Connection

Update settings about configuration, Target Framework, Deployment Mode, Target Runtime, etc.

Where the file(s) will be saved or placed.

Save

Look inside of the publish file.

Publish file

Some of the properties

Highlighted in Red color for removing unwanted files while publishing for the application, It will help to delete or not include the file specified in the property CopyToPublishDirectory=" Never".

Sample Files for three different environments.

Development

<?xml version="1.0" encoding="utf-8"?>
<!--
    https://go.microsoft.com/fwlink/?LinkID=208121.
-->
<Project>
    <PropertyGroup>
        <DeleteExistingFiles>true</DeleteExistingFiles>
        <LastUsedBuildConfiguration>Release</LastUsedBuildConfiguration>
        <LastUsedPlatform>Any CPU</LastUsedPlatform>
        <PublishProvider>FileSystem</PublishProvider>
        <PublishUrl>bin\Release\net8.0\publishtesting\</PublishUrl>
        <WebPublishMethod>FileSystem</WebPublishMethod>
        <_TargetId>Folder</_TargetId>
        <TargetFramework>net8.0</TargetFramework>
        <ProjectGuid>893a2fc8-43c9-4994-8e4e-a509924aeb10</ProjectGuid>
        <SelfContained>false</SelfContained>
        <EnvironmentName>Development</EnvironmentName>
    </PropertyGroup>
    <ItemGroup>
        <Content Update="appsettings.Testing.json" CopyToPublishDirectory="Never" />
        <Content Update="appsettings.json" CopyToPublishDirectory="Never" />
    </ItemGroup>
</Project>

Testing

<?xml version="1.0" encoding="utf-8"?>
<!--
https://go.microsoft.com/fwlink/?LinkID=208121.
-->
<Project>
    <PropertyGroup>
        <DeleteExistingFiles>true</DeleteExistingFiles>
        <ExcludeApp_Data>false</ExcludeApp_Data>
        <LaunchSiteAfterPublish>true</LaunchSiteAfterPublish>
        <LastUsedBuildConfiguration>Release</LastUsedBuildConfiguration>
        <LastUsedPlatform>Any CPU</LastUsedPlatform>
        <PublishProvider>FileSystem</PublishProvider>
        <PublishUrl>bin\Release\net8.0\publishtesting\</PublishUrl>
        <WebPublishMethod>FileSystem</WebPublishMethod>
        <_TargetId>Folder</_TargetId>
        <SiteUrlToLaunchAfterPublish />
        <TargetFramework>net8.0</TargetFramework>
        <ProjectGuid>893a2fc8-43c9-4994-8e4e-a509924aeb10</ProjectGuid>
        <SelfContained>false</SelfContained>
        <EnvironmentName>Testing</EnvironmentName>
    </PropertyGroup>
    <ItemGroup>
        <Content Update="appsettings.Development.json" CopyToPublishDirectory="Never" />
        <Content Update="appsettings.json" CopyToPublishDirectory="Never" />
    </ItemGroup>
</Project>

Production

<?xml version="1.0" encoding="utf-8"?>
<!-- https://go.microsoft.com/fwlink/?LinkID=208121. -->
<Project>
  <PropertyGroup>
    <DeleteExistingFiles>false</DeleteExistingFiles>
    <ExcludeApp_Data>false</ExcludeApp_Data>
    <LaunchSiteAfterPublish>true</LaunchSiteAfterPublish>
    <LastUsedBuildConfiguration>Release</LastUsedBuildConfiguration>
    <LastUsedPlatform>Any CPU</LastUsedPlatform>
    <PublishProvider>FileSystem</PublishProvider>
    <PublishUrl>bin\Release\net8.0\publish\</PublishUrl>
    <WebPublishMethod>FileSystem</WebPublishMethod>
    <_TargetId>Folder</_TargetId>
  </PropertyGroup>
  <ItemGroup>
    <Content Update="appsettings.Development.json" CopyToPublishDirectory="Never" />
    <Content Update="appsettings.Testing.json" CopyToPublishDirectory="Never" />
  </ItemGroup>
</Project>

Result comparison after publishing

Comparison

Deployment

Localhost

Testing

Json