Tôi có một ngoại lệ tùy chỉnh FilterAttribute như sau:Khi nào thì MVC ExceptionFilter sẽ được thực hiện so với trình xử lý lỗi cấp ứng dụng?
[AttributeUsage(AttributeTargets.Class | AttributeTargets.Method, AllowMultiple = true)]
public sealed class ExceptionLoggingFilterAttribute : FilterAttribute, IExceptionFilter
{
public void OnException(ExceptionContext filterContext)
{
if (filterContext == null)
{
throw new ArgumentNullException(nameof(filterContext));
}
if (filterContext.ExceptionHandled)
{
return;
}
// some exception logging code (not shown)
filterContext.ExceptionHandled = true;
}
Tôi đã đăng ký này trên toàn cầu trong FilterConfig.cs tôi
public static class FilterConfig
{
public static void RegisterGlobalFilters(GlobalFilterCollection filters)
{
filters?.Add(new ExceptionLoggingFilterAttribute());
}
}
Tôi cũng có một phương pháp Application_Error khai báo trong global.asax tôi. cs
protected void Application_Error(object sender, EventArgs e)
{
var exception = Server.GetLastError();
// some exception logging code (not shown)
}
- khi mã lọc ngoại lệ sẽ bị ảnh hưởng và khi wil l nó đi thẳng đến trình xử lý lỗi toàn cục trong phương thức Application_Error? (Tôi hiểu khái niệm ExceptionHandled và nhận ra rằng bằng cách đánh dấu điều đó như được xử lý trong bộ lọc của tôi, nó sẽ không xếp tầng tới trình xử lý lỗi toàn cục).
Một ngoại lệ mà tôi cho rằng sẽ nhấn bộ lọc - một HttpException cho 404, không nhấn vào bộ lọc nhưng không bị bắt gặp trong trình xử lý lỗi ứng dụng.
- Tôi đã thấy một số mẫu mã nơi mọi người sử dụng HttpContext.Current trong global.asax.cs để thực hiện một Server.TransferRequest đến một dạng xem lỗi cụ thể. Đây có phải là phương pháp hay nhất không? Sẽ tốt hơn nếu sử dụng phần CustomErrors trong phần system.web của web.config?