Created
May 30, 2021 16:21
-
-
Save manoj-choudhari-git/313fd46c586e11c98291f695ad424a5b to your computer and use it in GitHub Desktop.
.NET Core Web API - Exception Handling Middleware and Error Controller
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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