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.
With the announcement of .NET 5 and C# 9.0 with their amazing new features and great overall enhancements, the Software development community is getting an increased need and a growing heat to learn and build projects and apps using the latest unified framework.
One of the focus technologies is the ASP.NET Core Web API. 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.
Visual Studio 2019 and Dot Net 5
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.8.3
This version of Visual Studio 2019 supports the latest version of Dot Net SDK, which is 5.0
Note that you cannot use Visual Studio 2017 to develop Dot Net Core 3 or Dot Net 5 apps.
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 5 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 SDKs and runtimes, so you might notice newer versions of Dot Net SDK and Runtime throughout the upcoming days.
Dot Net 5 Runtime on Windows Server
Download and install the Dot Net Runtime 5.0 (Hosting bundle installer for Windows) into your hosting windows server. This should be a pretty simple step.
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
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 a Swagger UI.
Swagger UI is the documentation based on Open API standard that describes your http APIs in a clear and organized way and allows you and your API clients to visualize all the endpoints and test them as well. If you see the Swagger page for your APIs 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, then choose folder
press next, then 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, as shown below:
Press Finish. You should now see a new profile created for you with your publish settings, you can edit the profile name and any other setting
The Target runtime is set by default to Portable, since we know that our server will be a Windows Server based on 64-bit, then we need to change this setting to match our hosting platform.
So click on the edit Target runtime pen, then from the Target Runtime dropdown choose win-x64
Then press save. You will notice the target runtime is now modified. Press Publish to generate the deployable files of your Web API.
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 the same Swagger UI documentation of the weather controller endpoint 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