- Modern Web Development with ASP.NET Core 3
- Ricardo Peres
- 174字
- 2021-06-18 18:36:01
Error handling
In the previous chapter, we saw how to redirect to specific actions when an error occurs. Another option could be to leverage the IExceptionFilter and IAsyncExceptionFilter interfaces, one of the filter classes, to have the controller itself—or some other class—implement error handling directly.
In our controller, it's just a matter of implementing theIExceptionFilterclass, which only has one method,OnException:
public void OnException(ExceptionContext context)
{
var ex = context.Exception;
//do something with the exception
//mark it as handled, so that it does not propagate
context.ExceptionHandled = true;
}
In the asynchronous version, IAsyncExceptionFilter, the OnExceptionAsync method takes the same parameter but must return a Task.
In Chapter 10, Understanding Filters, we will learn more about the concept of filters. For now, it is enough to say that should any exception be thrown from an action in a controller implementing IExceptionFilter, its OnException method will be called.
The next topic is related to performance: response caching.
- Go Web編程
- Vue 3移動(dòng)Web開發(fā)與性能調(diào)優(yōu)實(shí)戰(zhàn)
- Oracle從入門到精通(第3版)
- NLTK基礎(chǔ)教程:用NLTK和Python庫(kù)構(gòu)建機(jī)器學(xué)習(xí)應(yīng)用
- Selenium Design Patterns and Best Practices
- Instant 960 Grid System
- Hands-On C++ Game Animation Programming
- Full-Stack React Projects
- Data Analysis with Stata
- Python時(shí)間序列預(yù)測(cè)
- Spring Security Essentials
- JQuery風(fēng)暴:完美用戶體驗(yàn)
- STM8實(shí)戰(zhàn)
- Java 9 with JShell
- Java EE輕量級(jí)解決方案:S2SH