ASP.NET Core 404 Error in IIS 10

I have a problem with an ASP.NET Core web application running on IIS 10. I am developing a single page application using AngularJS.

index.html loads fine, but backend requests don't work with 404 error code in IIS 10. From Visual Studio, it works fine with IIS Express.

Can anyone determine how I can fix the backend requests?

Here is my program.cs

public static void Main(string[] args) { var host = new WebHostBuilder() .UseKestrel() .UseContentRoot(Directory.GetCurrentDirectory()) .UseIISIntegration() .UseStartup<Startup>() .Build(); host.Run(); } 

And here is my Configure method from Startup.cs

 public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory) { app.UseDefaultFiles(); app.UseStaticFiles(); app.UseIdentity(); // Custom middleware for Angular UI-Router app.Use(async (context, next) => { if (!Path.HasExtension(context.Request.Path.Value) && context.Request.HttpContext.Request.Headers["X-Requested-With"] != "XMLHttpRequest" && context.Request.Method.ToUpper() != "POST" && context.Request.Method.ToUpper() != "PUT" && context.Request.Method.ToUpper() != "DELETE") { await context.Response.WriteAsync(File.ReadAllText(env.WebRootPath + "/index.html")); } await next(); }); app.UseMvc(routes => { routes.MapRoute( name: "default", template: "api/{controller=Home}/{action=Index}/{id?}"); }); } 
+16
c # iis asp.net-core
source share
4 answers

Your code runs on my machine with Kestrel. A good troubleshooting step is to find out if the problem is with your ASP.NET Core application or with the configuration of the IIS host.

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, navigate to the following two URLs. If they do not work, then something is wrong with your application. If they 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 
+11
source share

In my case, the problem was that my controller threw an exception, so the structure tried to use the exception handler page, which was not available, therefore, error 404, the controller itself generated 500 errors

+19
source share

For the good of seekers.

I was getting 404 when using IIS. I followed the correct procedure for publishing ( here ) and deploying as described here .

It took some time to figure this out, but in the end I found the answer, hidden in Rick Strol's blog .

In fact, when creating the application pool, as well as when setting "No managed code", I also had to go to additional settings and set the application pool identifier to "Network Service". This was fine under ApplicationPoolIdentity on my machine, but not on the machine on which I am deployed.

enter image description here

So, for clarity, my complete procedure was:

To create a package:

  1. Create the main dotnet site (I used Visual Studio 2017)
  2. Publish. Could use the VS publish function, but I used the CLR through the package manager. The team was:

    Dotnet publish -c Release -r win-x64 - Self -c reached

I had to use the win-x64 identifier, since we must be compatible with 64-bit Windows Server 2008.

Expand:

  1. Create a folder in C: \ inetpub \ wwwroot (e.g. 'testsite')
  2. Take the contents of the publication folder ({root} \ bin \ Release \ netcoreapp2.1 \ win-x64 \ publish) and copy it to the new "testsite" folder (or your equivalent).
  3. Install the main DotNet runtime (not SDK!) On the host machine.
  4. Open IIS. Right-click Application Pools, then Add Add Application Pool. Create it with the .NET CLR version for which "No Managed Code" is installed.
  5. (my machine did not need this step, but the server did it). Click on the application pools again. Right-click the new application pool and select Advanced Settings. Change the identity to "Network Service" (as shown in the figure above).
  6. Back at the top level of IIS, expand “Default Web Site,” right-click your website’s folder and select “Convert to Application”. Select a new application pool without managed code.
  7. Open a command prompt as admin and iisreset . This may only be necessary for the first time after installing the dotnet kernel runtime.
  8. Visit the site (e.g. http: // localhost / testsite )
+6
source share

Another very stupid, but likely, option is that the application was deployed in a different folder than what you expected (for example, the root of your server instead of a subfolder) due to erroneous deployment settings.

0
source share

All Articles