How to catch an exception and respond with a status code in .NET Core
Clash Royale CLAN TAG #URR8PPP How to catch an exception and respond with a status code in .NET Core I am running a .Net Core Web API project. I have a startup file (below). In the Startup.ConfigureServices(...) method, I add a factory method that creates an instance of IFoo. I want to catch any exceptions that the IFooFactory throws and return a better error message with a status code. At the moment I getting a 500 Error with the exception Message. Can anyone help? Startup.ConfigureServices(...) public interface IFooFactory { IFoo Create(); } public class FooFactory : IFooFactory { IFoo Create() { throw new Exception("Catch Me!"); } } public class Startup { public void ConfigureServices(IServiceCollection services) { services.AddScoped<IFooFactory,FooFactory>(); services.AddScoped(serviceProvider => { IFooFactory fooFactory = serviceProvider.GetService<IFooFactory>(); return fooFacto...