If you started developing with .NET Core and you can’t leave your Windows hosting server, then you need to learn how to set up the IIS for it. Follow these steps and you will be good to go.
Developing with .NET Core is not too different from developing with .NET Framework, but setting up the IIS to host a .NET Core application… ow… it needs some tricks.
I’m trying to be as clear as possible. For this article, I am using the source code that I wrote in my .NET Core for .NET Developers article. Click here to download it.
Installations on Windows Machine
Step 1
Before setting up the IIS, it’s required to install .NET Core Runtime. For this article, I’ve used version 2.2. Without that, .NET Core won’t work. Remember, maybe it’s possible to reboot Windows.
Step 2
After that, you need to install the .NET Core Windows Server Hosting bundle, and you can download it here. Without that, IIS won’t work with .NET Core. You will need to reboot Windows.
Changes in the .NET Core web project
Step 3
Open the FSL.NetCoreBasics.Mvc.csproj project file using notepad, locate the tag AspNetCoreHostingModel and change it to AspNetCoreHostingModelV2.
- <AspNetCoreHostingModelV2>InProcess</AspNetCoreHostingModelV2>
After that, you will need to restore NuGet package and compile the application.
Changes in the C# code
Step 4
In Visual Studio, open the project and, then open the Program.cs file, locate a method called Main and make the changes like the code below.
- public static void Main(string[] args)
- {
- var host = new WebHostBuilder()
- .UseKestrel()
- .UseContentRoot(Directory.GetCurrentDirectory())
- .UseIISIntegration()
- .UseStartup<Startup>()
- .Build();
- host.Run();
- }
You can delete the
CreateWebHostBuilder method because it isn’t necessary anymore.
Step 05
Open the Startup.cs file, locate a method called ConfigureServices and add the following commands:
- services.Configure<IISOptions>(options =>
- {
- options.AutomaticAuthentication = false;
- });
Adding a web.config file
Step 06
Yes my friend, it’s required to add a web.config file to our project to host a .NET Core application on IIS. Create that file and copy/paste the following code:
- <?xml version="1.0" encoding="utf-8"?>
- <configuration>
- <system.webServer>
- <handlers>
- <add name="aspNetCore"
- path="*"
- verb="*"
- modules="AspNetCoreModule"
- resourceType="Unspecified"/>
- </handlers>
- <aspNetCore processPath="%LAUNCHER_PATH%"
- arguments="%LAUNCHER_ARGS%"
- stdoutLogEnabled="false"
- stdoutLogFile=".\logs\stdout"
- forwardWindowsAuthToken="false"/>
- </system.webServer>
- </configuration>
Special thanks to Macoratti for that piece of code.
Setting up the IIS
The next step is to set up an Application Pool and a virtual directory (or website) in IIS to run a .NET Core application.
Step 7
Open the IIS, add a new Application Pool, then choose No Managed Code for .NET Framework version field and finally, select Integrated for Managed pipeline mode field.
Step 8
Add a new folder to host your application on IIS and create a virtual directory for it. Don’t forget to choose the Application Pool that you’ve created in Step 7.
Publishing on IIS
Step 9
Open the project in Visual Studio, click with the mouse’s right button on the project and choose the Publish option. Select the Folder tab on the left, fill or select your application folder and click Publish.
If you open Windows Explorer and navigate to your application folder, you will find all DLLs, the web.config file, and a wwwroot folder.
That’s your application published on IIS.
Testing the application
Step 10
Open your favorite browser and type http://localhost/FSL.NetCoreBasics.Mvc. Your .NET Core application will be started, running on IIS.
That’s it! Did you enjoy it? Have you found an error?
Please comment and share!
Thank you!