Publish And Deploy ASP.NET Core MVC On IIS

Introduction
 
ASP.NET Core is able to run as a standalone Application (out of process Console Application). It does not host inside IIS, which does not require IIS to run an application. ASP.NET Core app has its own self-hosted Web Server and processes the requests, which internally use the self-hosted Server instance. ASP.NET Core uses Kestrel Server to host the core Application but it does not support all the features of the Server such as IIS.
 
In older ASP.NET Web applications, everything is hosted inside IIS Worker process, which can also refer to an Application pool. The Application Pool hosts our Web Application and our Web Application is instantiated by the built-in ASP.NET hosting features in IIS. The ASP.NET Core Application does not run in the IIS worker process but runs as a separate out of process Console Application, which runs its own Web Server, using Kestrel Web Server. If we run on Windows, we may want to run Kestrel behind IIS to gain infrastructure features such as port forwarding via host headers, life cycle and certificate management.

ASP.NET Core Application is a standalone console Application that is invoked through the "dotnet" runtime command. It does not load into the IIS worker process but loads in the native IIS module called "AspNetCoreModule" which runs the console application. We need to install the AspNetCoreModule on the Server and it is part of ASP.NET Core Server Hosting Bundle.
 
 
 
All IIS applications are run on application pool. For ASP.net core, application pool must set to use No Managed Code. The Application pool acts only as a proxy to forward the requests. There is no need to instantiate a .NET runtime.
 
 
 
The primary job of AspNetCoreModule is to ensure that our Application gets loaded when the first request comes. All incoming Http request are handled by AspNetCoreModule and then it is routed to ASP.NET Core Application. Kestrel picks up the incoming request and passes into ASP.NET Core middleware pipeline that subsequently handles the request and passes to the Application logic, which is the same as the response of the Application which is pushback to the IIS and then Http client.
 
AspNetCoreModule is configured via the web.config file, which is stored in an Application root folder, which contains the startup command and argument. The dotnet is a startup command and our Application main DLL, which is used to launch application as aargument.
 
The Web.config file looks, as shown below.
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <configuration>  
  3. <system.webServer>  
  4.      <handlers>  
  5.       <add name="aspNetCore" path="*" verb="*" modules="AspNetCoreModule" resourceType="Unspecified"/>  
  6.     </handlers>  
  7.     <aspNetCore processPath="dotnet" arguments=".\FirstCoreApplication.dll" stdoutLogEnabled="false" stdoutLogFile=".\logs\stdout" forwardWindowsAuthToken="false"/>  
  8. </system.webServer>  
  9. </configuration>  
Publishing ASP.NET Core Application
 
To host our Application on IIS, we first need to publish it. There are two ways to publish .NET Core Application
  • Using CLI command dotnet publish.
  • Using Visual Studio Publishing feature.
Before publishing ASP.NET Core Application, we make sure that we are including all the required folders in PublishOptions node of project.json file. This node looks, as shown below.
  1. "publishOptions": {  
  2.   "include": [  
  3.     "Views",  
  4.     "web.config"  
  5.   ]  
  6. }  
Using CLI command “dotnet publish”

.NET Core Application also supports CLI. Using CLI tool, we can create, build and publish the .NET Core Application. The "dotnet publish" command compiles the Application and publishes the Application with the project dependencies (specified in project.json file) and puts it into a specified folder.
  1. >dotnet publish   
 
 
 
Using Visual Studio Publishing feature
 
To publish our application, using Visual Studio, the first step is to right click on the project which we want to to publish and select publish option. Next step is to select custom option and enter the profile name. Once the profile is created, we need to select publishing method. Here, we have four different publishing methods, namely File system, Web deploy, Web deploy pack and FTP. In this example, I have selected File System as publish option and also need to pass target location.
 
In the next step, we can select the configuration, target framework and some other publish option. When we click Publish button, Visual Studio publish requires file in to the selected folder.
 
 
 
Host publish folder on IIS Manually
 
To host ASP.NET Core Application on IIS, we must have Windows 7 or newer / Windows Server 2008 R2 or newer. To configure IIS on Windows machine, we need to turn on some IIS related features of Windows.
 
 
Once our Application is published, we can hook up IIS to the published folder. Now, I am going to create a virtual Application directory.
 
 
 
Now, I am creating ASPNETCore Application with .NET runtime "No Managed Code", as shown earlier.
Summary
 
This article helps us to understand, how to publish and deploy ASP.NET Core Application IIS. ASP.NET Core works on IIS, which is slightly different compared to the classic ASP.NET Application.