我正在使用 Elmah 在我的 MVC 应用程序上使用 Alex Beletsky's elmah-mvc 记录异常NuGet 包。
应用程序注册了一些全局过滤器,应用于每个调用的操作。
有没有办法在调用 Elmah.Mvc.ElmahController
时阻止应用其中一些过滤器?错误日志页面 (foo.com/elmah
) ?
当然,像下面这样的测试可以工作,但我正在寻找一种更优雅的方式,它不涉及修改过滤器(也不涉及 Elmah/Elmah MVC 的源代码)。甚至可能吗?
public class FooAttribute : FilterAttribute, IActionFilter
{
// ...
public void OnActionExecuting(ActionExecutingContext filterContext)
{
if (filterContext.Controller is ElmahController)
{
return;
}
// do stuff
}
}
ElmahController
在一个新的地方,我可以添加一个排除过滤器,但我不确定如何(如果可能)更改 web.config
引用此包装器而不是原始 Controller 。 最佳答案
您可以通过自定义 IFilterProvider
注册您的全局过滤器。 :
public class MyFilterProvider : IFilterProvider
{
public IEnumerable<Filter> GetFilters(ControllerContext controllerContext, ActionDescriptor actionDescriptor)
{
if (controllerContext.Controller is ElmahController)
{
return Enumerable.Empty<Filter>();
}
return ... the collection of your global filters
}
}
在你的
Application_Start
而不是调用:FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
你会调用:
FilterProviders.Providers.Add(new MyFilterProvider());
关于asp.net - 防止在 Elmah 操作上应用 ASP.NET MVC 全局过滤器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28193082/