Deploying ASP.NET Core Web Api on IIS is an easy task once you understand and follow the correct steps to prepare your development and deployment environment.
Following the announcement by Microsoft’s Visual Studio team of some major updates on their flagship products including Dot Net Core 3.1, Visual Studio 16.6, C# 8.0 and others, I thought that would be helpful to get you started with learning about these great features and products. I know these are really huge topics to talk about, but I will try to write about different topics within my upcoming articles.
For this article, I want to share with you a guide about how to deploy ASP.NET Core Web Api on IIS,
Visual Studio 2019 and Dot Net Core 3.1
The first and most important thing that you need to download and install is the latest version of Visual Studio 2019, the version at the time of writing is VS 16.6
This version of Visual Studio 2019 supports the latest version of Dot Net Core SDK, which is 3.1.5 at the time of writing.
Note that you cannot use Visual Studio 2017 to develop Dot Net Core 3.x apps. You must use Visual Studio 2019.
In the normal cases, you don’t have to download the SDK separately since you have installed the latest version of Visual Studio 2019, however, if you open Visual Studio 2019 and do not the see option of Dot Net Core 3.x for whatever reason, then you can download the Dot Net Core SDK from the official site of Microsoft.
There is a massive effort being put by the open source community alongside Microsoft to keep updating and improving the Dot Net Core SDKs and runtimes, so you might notice newer versions of Dot Net Core SDK and Runtime throughout the upcoming days.
Dot Net Core Runtime on Windows Server
Download and install the Dot Net Core Runtime 3.1.5 (Hosting bundle installer for Windows) into your hosting windows server.
This should be a pretty simple step.
If you do not have access to do this installation however,
you can either ask your hosting server admin to do it for you,
Or otherwise, you will have to deploy your ASP.NET Core Web Api as a self-contained deployment, which won’t require an installation of the Dot Net Core Runtime within the hosting server.
All the required runtime libraries will be included as part of the App deployment container.
This gives you the confidence on your app once it is on production that it would behave exactly as you have tested it, that no update on the server’s Dot Net Core Framework would affect your app,
but the drawback of it is that your deployment size will be significantly larger that the framework-dependent deployment type (the type that relies on the shared Dot Net Core Runtime within the server).
Enable IIS on Server
Another thing that have to be enabled in your server, is the Web IIS feature. You might already have this enabled in your hosting server, however, you have to verify that it has been installed there.
From your Windows Server , Open Server Manager, then IIS, then Manage and select ‘Add Roles and Features’, then go to features, then see if the Web IIS checkbox is enabled, if not, then proceed with installing it.
IIS Application Pool
For the Dot Net Core Apps to work under IIS, we will have to create a new application pool with the no managed code option.
The IIS Application pool will not have any effect on the runtime of the Dot Net Core Apps, it only works as a reverse proxy
To create the application pool, open your IIS manager, then navigate to Application Pools, then click ‘Add Application Pool’, a dialog will appear, Give it a name such as ‘DotNetCore’ ,
In the .Net Framework version, choose ‘No managed code’, then leave the last option as-is, and press Ok.
You should be able to see your newly created application pool listed in the application pools panel.
Since we have the application pool ready, now we need to create a new website under IIS Sites. So from the left-side menu of IIS manager, right-click on sites, then choose ‘Add Website’ .
From the dialog, give it a name like ‘My Asp Net Core Web Api’, assign it to the newly created application pool ‘DotNetCore’, and select the physical path of your website’s folder, make sure that you create a folder anywhere you want within your server’s drive.
In the binding section, make sure to provide it a new binding port for http , such as 5100 and https such as 6100, or whatever you find appropriate according to your current IIS, you might have other websites with different assigned ports.
Create ASP.NET Core Web Api Project in VS2019
Open Visual Studio 2019, create a new project of type ASP.NET Core Web Application,
Give it a name and folder path
Then select Api as project template, then finally press create.
Once visual studio finishes creating the project, you can try and run the Api.
Press the run button on Visual Studio, it will start building the Dot Net Core Web Api project, the Api will be hosted under IIS Express and it will open a local browser with the localhost and some port will be assigned along with the default controller and action and with some results on the browser.
If you see some json output on browser, it means that your first ASP.NET Core Web Api is up and running on your machine 🙂
Publish your ASP.NET Core Web Api
Now let’s make this work fine on your windows server as well.
We have everything ready on the server and the IIS to run our website, so we just need to publish our ASP.NET Core Web Api publish files to our empty folder on the server.
Right-click on the project name and choose publish,
After that, from the publish settings, choose folder location, and put the path as the new folder within your server (if the new folder is accessible via a shared link from your machine).
Or set the publish path to your local machine, then you can just copy paste the files into your folder on your server.
Next click on Finish, you should now see the new publish profile with all the default details that you can edit:
Click on the pencil icon beside Target runtime to open the publish profile settings.
Here we want to change the Target runtime to win-x64, because we know the target server that we will using as the host for our ASP.NET Core Web Api project.
We will keep the deployment mode as framework-dependent (since we have already installed the Dot Net Core Runtime 3.1.5 inside the Windows Server).
Now, click on Target Runtime and choose win-x64 and press Save.
Finally press Publish
Now Visual studio will package your app and write all the necessary files into the new folder.
Once you get the ‘published successfully’ message within your Visual Studio or see the above files with the correct dates modified, switch back to your windows server, verify that the new folder has the published files, and then go to your newly created website under IIS manager, right click on it then click browse, then add the correct controller name on the url.
You should see some random json weather-related results as you’ve seen in your machine’s browser.
Congrats! You have created and deployed your first ASP.NET Core Web Api on IIS
Note: You can enable ASP.NET Core Web Api Logging by opening the Web.config from the published folder, and changing the stdoutLogEnabled property value to “true”,
then you will start seeing log files within a new folder under your published Api files.
Let me know in the comments if everything works fine.
For further reading, check this article about the latest updates on Dot Net Core and Visual studio 2019
You can also check this tutorial about Hosting An ASP.NET Core Web Application in IIS
Check my article to learn how to Build RESTful APIs using ASP.NET Core and Entity Framework Core.
My wonderful reader/learner, you have reached the secret area.
Enjoy this brilliant masterpiece:
Bach – Violin Sonata No. 1 in G minor, BWV 1001 Played by Arthur Grumiaux