Publishing an ASP.NET Core Web App to Adaptive Web Hosting Print

  • 2

1. Create a Database

  1. In the Adaptive Web Hosting Plesk control panel, create an MS SQL database.

    • Choose MSSQL 2022 / 2019.
    • Give it a name.
    • Example database name and info:
      DB_12345_home
      MSSQL: SQL2019-001.adaptivewebhosting.com
      Username: DB_12345_home_admin

  2. Create your connection string:

    • Choose an ASP.NET connection string. Should look like this:
      “Data Source=SQL2019-001.adaptivewebhosting.com;Initial Catalog=DB_12345_home;User Id=DB_12345_home_admin;Password=YOUR_DB_PASSWORD;”

  3. Check the database using one of these tools:

    • Microsoft SQL Server Management Studio
    • Visual Studio’s Server Explorer
    • DBeaver, a useful cross-platform free, open-source database manager.

2. Prepare the Web App

There are two ways you can deploy your app:

  • Framework-dependent deployment
    The .Net Core run-time package is installed on the server and so your app uses the run-time environment that is already there.

  • Self-contained deployment
    The .NET Core run-time package is deployed along with your app.

We are going to use the self-contained approach since it can be tricky to try to change the build settings of your project in Visual Studio so that it is targeting one of the versions of .NET Core that is installed on the Adaptive Web Hosting servers. Because of that, there isn’t much to do in this step except to configure the database connection string for the production server

Configuring the production connection string

  • Add two new appsettings files to your project:

    • appsettings.development.json
      Move the connection strings for your local machine to this file.

    • appsettings.production.json

      • Put the connection string for the hosted database here. Put xxxx or some other place-holder in place of the password. Be sure the connection string has the same name as one that is loaded in the Startup class
      • Example:
        {
        "ConnectionStrings": {
        "MsSqlConnection": "Data Source=SQL2019-001.adaptivewebhosting.com,1433;
        Initial Catalog=DB_12345_home;User
        Id=DB_123456_home_admin;Password=xxxxxx;"
        }
        }
      • Put a copy of the same production connection string in your default appsettings.json. This is only so that the Visual Studio Publish wizard will see it.

  • Set your local machine’s ASPCORENET_ENVIRONMENT environment variable to Development. The environment variable on the production server will have already been set to Production

  • Note: The Visual Studio Publish tool won’t recognize connection strings unless they are in a top-level JSON object named “ConnectionStrings”.

When you are running your app on your development machine, the default appsettings.json and appsettings.development.json will be read. When your app runs on the production machine, appsettings.json and appsettings.production.json will be used.

3. Deploy the Web App

There are several different methods you can use to deploy your app using the Visual Studio Publish wizard:

  • WebDeploy
    This is a proprietary Microsoft protocol for uploading files to a server. It’s supported by Adaptive Web Hosting and is a good option.

  • FTP
    This, of course, is a universally supported protocol and is another good option.

  • File System
    This option simply puts the output files in a folder of your choice on your machine. From there you can manually copy the files to the server.

We will use the File System option. The reasons for this are:

  • Some ISPs or company firewalls block WebDeploy.

  • The FTP option in Visual Studio 2017-2019 appears to be buggy! Sometimes it says it uploaded the files successfully when actually it hasn’t.

A. Create a publish profile

Here are the steps to create a publish profile for your app:

  1. Start by right-clicking on your project name in the Visual Studio Solution Explorer and selecting Publish.

  2. Click on Configure and in the connection dialog box select File System as the publish method and enter the location where you want to put the output files
  1. Select Settings and enter settings similar to those in the image below. Here are some things that may be different:

    • Target Framework–This will be set automatically depending on the target in your Visual Studio project

    • Databases, connection strings–the connection string names will come from your project..  Be sure to enter the password.

    • Entity Framework Migrations–Copy the connection string from Databases, with the password, and put it here. When you run the Publish wizard it will connect to your online database and apply the latest migration. This is equivalent to running dotnet ef database update on your local machine.

      • Be sure your connection string contains the full URL for your online database, for example: 
        SQL2019-001.adaptivewebhosting.com

B. Publish the app

  1. Click Save, then click on Publish and your app will be published to the folder your chose.
  2. Now you need to copy the files to your online server. You can do that using one of two ways:
    • Use the File Manager that is part of the Adaptive Web Hosting Plesk control panel.
      • On your local machine, zip all the files in the folder where you published your site. Just zip the files, not the enclosing folder.
      • In the Adaptive Web Hosting Plesk file manager:
        • Upload the zip file to your home folder.
        • Unzip the files.
    • Use an FTP client like FileZilla to upload the files

C. Run your app!

Point your browser at the URL for your site and you should see your home page!

 

Basic troubleshooting

If you see an error page instead of your home page, here are some things to check:

  • Did your database tables get created by the Visual Studio Publish wizard? You can check this by using the a database viewer to see if your database has the right tables.

    • If the tables weren’t created, you can check the connection string in the publish wizard (make sure it has a password) and try publishing again.

  • Did appsettings.production.json get uploaded to the home folder of your web site? You can check this using the File manager in the SmarterASP.NET Control panel.

  • Does appsettings.production.json contain the correct connection string and database password? This file should have been updated by the publish wizard. You can check it’s contents by using the File Manager in the Adaptive Web Hosting Plesk control panel to download it, or download it using an FTP client.

  • Did web.config get uploaded to the home folder of your web site?

    This file is required to configure IIS to run an ASP.NET Core web site. The file was generated by the Publish wizard.

Advanced troubleshooting

You can do further troubleshooting by enabling error logging from your ASP.NET Core web app. You need to do this:

  • Edit web.config to set stdoutLogEnabled to true, like this:
    <aspNetCore processPath=".\MyWebAPP.exe" stdoutLogEnabled="true" stdoutLogFile=".\logs\stdout" />
  • Ensure that the folder containing your web site has read/write permissions. (IIS will create a logs sub-folder there.)

Now, turn off your site off and turn it back on and refresh your home page in your browser. Check the logs folder and see if there is now a log file there. If there is, download it and see what kind of clues are in it.

 

Note: All web files reside in the /httpdocs folder which is your root folder by default.


Was this answer helpful?

« Back