ASP.NET Core 404 Error on IIS 10

Mate Zabo picture Mate Zabo · Jul 22, 2016 · Viewed 62.1k times · Source

I have a problem with ASP.NET Core web application running on IIS 10. I am developing a Single Page Application with AngularJS.

The index.html loads perfectly but the backend requests are failing with 404 error code on the IIS 10. From Visual Studio with IIS Express it works perfectly.

Can anyone spot how can I fix the backend requests?

Here's 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's 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?}");
    });
}

Answer

mohas picture mohas · Jun 23, 2017

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