Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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

like image 307
Rak She'ela Avatar asked Dec 17 '17 14:12

Rak She'ela


People also ask

Can IIS store the .NET Core process directly without Dotnet process?

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.

How do I host a .NET Core application?

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.


2 Answers

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>
    
like image 44
Robert Slaney Avatar answered Oct 29 '22 00:10

Robert Slaney


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"
        }
    }
}
like image 174
RANSUROTTO Avatar answered Oct 29 '22 02:10

RANSUROTTO