Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
585 views
in Technique[技术] by (71.8m points)

asp.net core webapi - Error controller not being hit due to Fallback controller

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");
    }

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
等待大神答复

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...