I'm building an api app. In the old ASP.NET there was Application_Error() to catch all unhandled exceptions
protected void Application_Error()
{
var exception = Server.GetLastError();
_logger.FatalException("Fatal error.", exception);
}
What should be used in ASP.NET 5?
Use the UseExceptionHandler middleware in ASP.NET Core So, to implement the global exception handler, we can use the benefits of the ASP.NET Core build-in Middleware. A middleware is indicated as a software component inserted into the request processing pipeline which handles the requests and responses.
try catch finally 2. Use error events to deal with exceptions within the scope of an object. Page_Error Global_Error Application_Error 3. Use custom error pages to display informational messages for unhandled exceptions within the scope of a Web application.
The solution is to add a custom filter. This is how it could be done:
public void ConfigureServices(IServiceCollection services)
{
services.AddMvc(options => options.Filters.Add(new MyExceptionFilter()));
}
Now the custom filter should derive from IExceptionFilter:
public class MyExceptionFilter : ActionFilterAttribute, IExceptionFilter
{
public void OnException(ExceptionContext context)
{
}
}
Unfortunately, it doesn't catch exceptions during Startup.Configuration()
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With