CS296N Web Development 2: ASP.NET                                              

Deploying a Web App to a Windows Server on a Shared Hosting Service

Topics by week
1. Intro to course and Identity
6. Midterm, Load Testing and Performance
2. Authentication and Authorization 7. Creating a Web Service
3. Claims and Third Party Authentication
8. Consuming a Web Service
4. Web Security
9. Docker Containers
5. Publishing to a Production Server 10.  Microservices

Contents


Overview

We will be deploying (publishing) a web app to a production server running the Windows Server operating system and using IIS (Internet Information Server) as a web server. This example uses a commercial shared hosting service.

These are the steps to publish an ASP.NET Core web app to Smarter ASP.NET

  1. Create an account
  2. Set up a database
  3. Prepare your app for production
  4. Publish your web app

Detailed Instructions

1. Create an Account

Smarter ASP.NET offers a free 60 day account without a credit card.

2. Create a Database

  1. In the control panel, create a MSSQL database
    • I chose MSSQL 2016, but it probably doesn't matter which version you use.
    • I named it the same as my web site--just so it would be easy to remember.
    • Example database name and info:
      DB_A452B1_home
      MSSQL: sql5030.site4now.net
      Username: DB_A452B1_home_admin
  2. Get the connection string by clicking on "Connection String Examples" at the bottom of the database page in the control panel
    • Choose ASP.NET connection string. Mine looks like this:
      "Data Source=SQL5030.site4now.net;Initial Catalog=DB_A452B1_home;User Id=DB_A452B1_home_admin;Password=YOUR_DB_PASSWORD;"
  3. Check the database using one of these tools:
    • The online database manager: Webbase MS SQL Server Manager
    • Microsoft SQL Server Management Studio
    • Visual Studio Server Explorer
    • DBeaver, a useful cross-platform free, open-source database manager.

3. Prepare the Web App

  1. Use the User Secret Manager to hide the password in the production database connection string.
    • Right-click on the project, select Manage User Secrets. This adds the following section to the .csproj file:
      <PropertyGroup>
          . . .
          <UserSecretsId>bfda0ff1-3074-461a-bde3-33e61533b2fc</UserSecretsId>
  2. Configure a production connection string
    • Add a new file to your project named appsettings.production.json
    • In this file add a section for connection strings and add the connection string for your production database. Be sure the connection string has the same name as one that is loaded in startup.cs.
      Example:
      {
        "ConnectionStrings": {
          "MsSqlConnection": "Data Source=SQL5004,1433;
              Initial Catalog=DB_A451DA_BookInfo2;User Id=DB_A451DA_BookInfo2_admin;Password=Secret-1;"
        }
      }
    • 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. For instructions for setting environment variables, see Set the Environment.
    • Note: The Visual Studio Publish tool won't recognize connection strings unless they are in a top-level object named "ConnectionStrings".

  3. If you will be doing a Framework Dependent installation, set the target framework in your Visual Studio project for one that is supported by the production server.
    • The production server may not support the latest version of ASP.NET Core. For example, the latest version that Smarter ASP.NET currently supports is 2.0.0.
    • You need to find a version for which you have an SDK installed on your development machine and which is supported on production server. In my case, using Smarter ASP.NET, that was version 2.1.2.
    • Set the target framework in the build properties of your project to 2.1.2
    • Set the target framework in the Publish dialog to 2.1.2 as well.

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

4. Deploy the Web App

Create a publish profile in VS 2017

Follow the instructions in the knowledge base article, How to publish ASP.NET Core web application via Visual Studio 2017.

Publish the Web App!

Troubleshooting

Here are my raw troubleshooting notes.


FAQ

Q: Is there a way to tell what settings were used for publishing a site by looking at the published binaries?
A: Yes, for some things:

Q: If my database has empty tables, how do I get seed data into the tables?
A: If your seed data method is called from the Startup file, then you just need to turn off your web site and turn it back on.

Q: If my firewall is blocking Visual Studio from running the migrations SQL script when I publish my web app, is there another way to create the tables in my database?
A: Yes, you can use the attach .mdf file option in the database page of the SmarterASP.NET control panel to upload the database file from your local machine.


References