ASP.NET Core 404 Error on IIS 10

In my case the problem was that my controller threw an exception, thus the framework tried to use the exception handler page which was no available, thus the 404 error, the controller itself was throwing 500 error


For the benefit of searchers.

I was getting a 404 when Using IIS. I had followed the correct procedure for publishing (here) and deployment as detailed here.

It took some time to figure out, but I eventually found the answer hidden in a Rick Strahl blog post.

Basically, when making the application pool, as well as setting it to 'No Managed Code', I also needed to go into the advanced settings and set the Application Pool Identity to 'Network Service'. It was fine under ApplicationPoolIdentity on my machine, but not on a machine I deployed to.

enter image description here

So, for clarity, my full procedure was:

To create package:

  1. Create dotnet core website (I used Visual Studio 2017)
  2. Publish. Could have used VS's publish function, but I used CLR via the package manager. The command was:

    dotnet publish -c Release -r win-x64 --self-contained

I had to use the win-x64 identifier as we have to be compatible with 64-bit Windows Server 2008.

To deploy:

  1. Make a folder in C:\inetpub\wwwroot (e.g. 'testsite')
  2. Take the contents of the publish folder ({root}\bin\Release\netcoreapp2.1\win-x64\publish) and copy it to the new 'testsite' folder (or your equivalent).
  3. Install the dotnet core runtime (not SDK!) on the host machine.
  4. Open IIS. Right click 'Application pools', then 'Add Application Pool'. Create one with the .NET CLR Version set to 'No Managed Code'.
  5. (my machine didn't need this step, but server did).Click on Application pools again. Right click your new App Pool and Choose 'Advanced Settings'. Change the identity to 'Network Service' (as shown in the picture above).
  6. Back at top level IIS, Expand 'Default Web Site', right click the folder of your website and choose 'Convert to application'. Choose your new App Pool with No Managed Code.
  7. Open command prompt as admin and iisreset . You should only need this the first time after you've installed the dotnet core runtime.
  8. Visit the site (e.g. http://localhost/testsite)

You code is working on my machine with Kestrel. A good troubleshooting step is to find out whether the problem is with your ASP.NET Core application or with your IIS Hosting configuration.

Try this from the root of your project.

dotnet restore
dotnet run

You will see something like this:

Hosting environment: Production
Content root path: C:\MyApplicationPath
Now listening on: http://localhost:5000
Application started. Press Ctrl+C to shut down.

In your browser go to the following two URLs. If they don't work, then something is wrong with your application. If they do work, something is wrong with your IIS hosting.

localhost:5000        // you will see your index.html page
localhost:5000/api    // you will see your default routes output

I'm a noob with windows IIS deployments so this answer might seem obvious to some people.

I was missing a key file - web.config which IIS needs in order to run your web application. I used the web.config configurations specified here:

https://docs.microsoft.com/en-us/aspnet/core/host-and-deploy/iis/web-config?view=aspnetcore-5.0

Specifically, the configurations for ASP.NET Core Module.