How to host my ASP.NET core site on IIS (in development environment, without deploy / publish)

有些话、适合烂在心里 提交于 2020-05-26 02:44:07

问题


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.

  1. 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 &quot;$(SolutionDir)published-site&quot; (&#xD;&#xA;  echo. 2&gt;&quot;$(SolutionDir)published-site\app_offline.htm&quot;&#xD;&#xA;)" />
    </Target>
    
  2. 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 &quot;$(SolutionDir)published-site\app_offline.htm&quot; (&#xD;&#xA;  del &quot;$(SolutionDir)published-site\app_offline.htm&quot;&#xD;&#xA;)&#xD;&#xA;&#xD;&#xA;dotnet publish --no-build --no-restore &quot;$(ProjectPath)&quot; -c $(ConfigurationName) -o &quot;$(SolutionDir)published-site&quot;" />
    </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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!