
I like my ASP.NET Core controllers and actions very clean and I recommend to not catch all Exceptions inside your Action.
You can have it much easier and simplier - with your custom Exception Middleware.
For exemaple I want to catch all NotImplementedException (I hope you do not have any of them in your final code) and return the HTTP status code 501 (HttpStatusCode.NotImplemented) I can just use this middleware implementation:
1 public class NotImplementedExceptionMiddleware
2 {
3 private readonly RequestDelegate _next;
4 private readonly ILogger<NotImplementedExceptionMiddleware> _logger;
5
6 public NotImplementedExceptionMiddleware(RequestDelegate next, ILoggerFactory loggerFactory)
7 {
8 _next = next ?? throw new ArgumentNullException(nameof(next));
9 _logger = loggerFactory?.CreateLogger<NotImplementedExceptionMiddleware>() ?? throw new ArgumentNullException(nameof(loggerFactory));
10 }
11
12 public async Task Invoke(HttpContext context)
13 {
14 try
15 {
16 await _next(context);
17 }
18 catch (NotImplementedException ex)
19 {
20 if (!context.Response.HasStarted)
21 {
22 int errorCode = (int)HttpStatusCode.NotImplemented;
23
24 context.Response.Clear();
25 context.Response.StatusCode = errorCode;
26 context.Response.ContentType = "application/json";
27
28 // this is a sample error result object
29 ErrorResult error = new ErrorResult("Not implemented", ex.Message);
30 await context.Response.WriteAsync(error.ToJson());
31 }
32 }
33 }
34 }
1A simple extention to register the middleware cleaner:
2
3 public static class NotImplementedExceptionMiddlewareExtensions
4 {
5 public static IApplicationBuilder UseNotImplementedExceptionMiddleware(this IApplicationBuilder builder)
6 {
7 return builder.UseMiddleware<NotImplementedExceptionMiddleware>();
8 }
9 }
And register this middleware:
1 // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
2 public void Configure(IApplicationBuilder app, IHostingEnvironment env, IPingPongService pingPongService)
3 {
4 if (env.IsDevelopment())
5 {
6 app.UseDeveloperExceptionPage();
7 }
8
9 // Use Middleware
10 app.UseNotImplementedExceptionMiddleware();
11
12 app.UseMvc();
13 }
Done.
Related articles

Feb 03, 2018 - 4 min read
What's new in .NET Core, ASP.NET Core and EF Core 2.1
Yesterday the Microsoft Teams have updated their roadmaps of .NET Core, ASP.NET Core and EF Core in focus of the next version 2.1:

Mar 26, 2017 - 1 min read
Better exception details with ASP.NET Core
By default, ASP.NET Core suppresses Exceptions on Startup on external environments. So you only get Exceptions in Startup on localhost.

Sep 17, 2016 - 2 min read
ASP.NET Core - Teil 2: die Middleware
Bereit bei ASP.NET 4 konnte man mit Hilfe von OWIN - Open Web Interface for .NET - Middleware-Implementierungen sehr einfach und robust …
Let's Work Together
Looking for an experienced Platform Architect or Engineer for your next project? Whether it's cloud migration, platform modernization or building new solutions from scratch - I'm here to help you succeed.
