For this article, I want to share with you a guide about how to deploy ASP.NET Core Web API on IIS. I walk you through installing Visual Studio 2019, the .NET Core Runtime 3.0. I also make sure I walk you through enabled in your server (Web IIS), creating a new application pool with the no managed code option, creating a ASP.NET Core Web API Project, and publishing your ASP.NET Core Web API.
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 few days ago by Microsoft’s Visual Studio team of some major updates on their flagship products including Dot Net Core 3.0, Visual Studio 16.3, 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.0
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.3.
This version of Visual Studio 2019 supports the latest version of Dot Net Core SDK, which is 3.0. Note that you cannot use Visual Studio 2017 to develop Dot Net Core 3 apps.
In 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 see the option of Dot Net Core 3 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.0 (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 has 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.
IIS Website
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
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 app.
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 Advanced… link under the folder location.
Then choose the deployment mode as framework-dependent (since we have already installed the Dot Net Core Runtime 3.0 inside the Windows Server).
Choose target Runtime as win-x64 (this should be based on the target machine’s OS version), then 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.
There you go, you have up and running ASP.NET Core web API published on IIS.
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.
History
- 6th February, 2020: Initial version