Skip to content

Instantly share code, notes, and snippets.

@manoj-choudhari-git
Created May 30, 2021 16:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save manoj-choudhari-git/313fd46c586e11c98291f695ad424a5b to your computer and use it in GitHub Desktop.
Save manoj-choudhari-git/313fd46c586e11c98291f695ad424a5b to your computer and use it in GitHub Desktop.
.NET Core Web API - Exception Handling Middleware and Error Controller
// ErrorController.cs
[ApiController]
public class ErrorController : ControllerBase
{
// URL for this API - /api/error
[Route("/error")]
public IActionResult Error() => Problem();
}
// Startup.cs
public class Startup
{
public void ConfigureServices(IServiceCollection services)
{
services.AddControllers();
}
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
// Apply ExceptionHandler middleware
app.UseExceptionHandler("/api/error");
// Other code is not shown here
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment