I'm running a .NET Core 3.1 Web API project and below is my pipeline. The problem I face is that when an error occurs, I want the error controller to be hit and the error to be logged. However, what is returned is the html from the fallback controller. Can you help me fix this routing issue?
app.UseExceptionHandler("/error");
app.UseDefaultFiles();
app.UseStaticFiles();
app.UseSwagger();
app.UseSwaggerUI(c => c.SwaggerEndpoint("/swagger/v1/swagger.json", "KYCDB API"));
app.UseRouting();
app.UseCors();
app.UseAuthentication();
app.UseAuthorization();
app.UseEndpoints(endpoints =>
{
endpoints.MapControllers();
endpoints.MapFallbackToController("Index", "Fallback");
});
app.UseHealthChecks("/api/monitor");
this is my error controller
[ApiController]
public class ErrorController : ControllerBase
{
private readonly ILogger<ErrorController> logger;
public ErrorController(ILogger<ErrorController> logger) => this.logger = logger;
[Route("/error")]
[NonAction]
public IActionResult Error()
{
var exception = HttpContext.Features.Get<IExceptionHandlerFeature>();
logger.LogError(exception.Error.Message, exception);
return Problem(detail: exception.Error.Message);
}
}
and the fallback controller
public class FallbackController : Controller
{
public ActionResult Index() => PhysicalFile(Path.Combine(Directory.GetCurrentDirectory(), "wwwroot", "index.html"), "text/HTML");
}
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…