ASP.NET Core basics · interview question
Put these ASP.NET Core middleware registrations in the recommended order in Program.cs.
OrderingMedium
Put the items in the correct order.
- app.UseHttpsRedirection()
- app.UseAuthentication()
- app.UseExceptionHandler("/error")
- app.MapControllers()
- app.UseAuthorization()
- app.UseRouting()
Answer and explanation
Correct answer
1. app.UseExceptionHandler("/error")
2. app.UseHttpsRedirection()
3. app.UseRouting()
4. app.UseAuthentication()
5. app.UseAuthorization()
6. app.MapControllers()
Why
Exception handling goes first so it can catch errors from everything after it. Routing must run before authentication/authorization so endpoint metadata (like [Authorize]) is known, and authentication must come before authorization. Endpoints are mapped last.
References
Test yourself on ASP.NET Core basics
A free test with instant, explained results. No account needed.