问题
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://docs.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
回答1:
If you need to debug an application, you can configure the ASP.NET Core application as the default IIS startup.
reference: https://docs.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"
}
}
}
回答2:
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>
回答3:
Without deploy/publish it is not possible. But you could consider using auto deployment as a workaround. For instance you could use some extensions like Auto Deploy, or by extending the MSBuild process.
来源:https://stackoverflow.com/questions/47855984/how-to-host-my-asp-net-core-site-on-iis-in-development-environment-without-dep