I used to host my ASP.NET sites (not core) on IIS locally, in my development environment.
Doing it helped me avoiding from using IIS Express (It's very uncomfortable to start & stop IIS express every time).
In this way all I had to do is to rebuild and refresh the site.
My goal is to work with ASP.NET core in the same way.
I read this: https://learn.microsoft.com/en-us/aspnet/core/publishing/iis?tabs=aspnetcore2x and it didn't go well.
What I'm looking for is to do it without deploy / publish after every C# code change (that requires building).
I use ASP.NET core 2 & IIS 10
With ASP.NET Core 2.2 there's now an In Process hosting model on IIS which hosts ASP.NET Core directly inside of an IIS Application pool without proxying to an external dotnet.exe instance running the . NET Core native Kestrel Web Server.
In general, to deploy an ASP.NET Core app to a hosting environment: Deploy the published app to a folder on the hosting server. Set up a process manager that starts the app when requests arrive and restarts the app after it crashes or the server reboots.
You can take advantage of the app_offline.htm feature of the ASPNETCore module and incorporate this into your build process.
But you need to publish your site and run IIS site/virtual app from that folder, not the project folder.
In the example below, we're publishing the site to a folder "published-site", relative to the sln location, but only for a Debug build.
I'm also assuming that the build process takes longer than it takes to spin down the process.
Create a PreBuild target that creates the app_offline.htm file in the publish folder.
<Target Name="PreBuild" BeforeTargets="PreBuildEvent" Condition="'$(Configuration)'=='Debug'">
<Exec Command="IF EXIST "$(SolutionDir)published-site" (
 echo. 2>"$(SolutionDir)published-site\app_offline.htm"
)" />
</Target>
Create a PostBuild target that deletes the app_offline.htm file and then calls dotnet publish.
<Target Name="PostBuild" AfterTargets="PostBuildEvent" Condition="'$(Configuration)'=='Debug'">
<Exec Command="IF EXIST "$(SolutionDir)published-site\app_offline.htm" (
 del "$(SolutionDir)published-site\app_offline.htm"
)

dotnet publish --no-build --no-restore "$(ProjectPath)" -c $(ConfigurationName) -o "$(SolutionDir)published-site"" />
</Target>
If you need to debug an application, you can configure the ASP.NET Core application as the default IIS startup.
reference: https://learn.microsoft.com/aspnet/core/host-and-deploy/iis/development-time-iis-support#configure-the-project
Create a new launch profile to add development-time IIS support. In Visual Studio's Solution Explorer, right-click the project and select Properties. Select the Debug tab. Select IIS from the Launch dropdown. Confirm that the Launch browser feature is enabled with the correct URL.
Or
Alternatively, manually add a launch profile to the launchSettings.json file in the app:
{
"iisSettings": {
"windowsAuthentication": false,
"anonymousAuthentication": true,
"iis": {
"applicationUrl": "http://localhost/WebApplication2",
"sslPort": 0
}
},
"profiles": {
"IIS": {
"commandName": "IIS",
"launchBrowser": "true",
"launchUrl": "http://localhost/WebApplication2",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With