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?}");
});
}
Resolution. To resolve this problem, verify that the file requested in the browser's URL exists on the IIS computer and that it is in the correct location. Use the IIS Microsoft Management Console (MMC) snap-in to determine where the file requested must exist in the IIS computer's file system.
NET Core Library, and the ASP.NET Core Module. The module allows ASP.NET Core apps to run behind IIS. If the Hosting Bundle is installed before IIS, the bundle installation must be repaired. Run the Hosting Bundle installer again after installing IIS.
404 error codes are generated when a user attempts to access a webpage that does not exist, has been moved, or has a dead or broken link. The 404 error code is one of the most frequent errors a web user encounters. Servers are required to respond to client requests, such as when a user attempts to visit a webpage.
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.
So, for clarity, my full procedure was:
To create package:
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:
iisreset
. You should only need this the first time after you've installed the dotnet core runtime.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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With