阻止对Elmah操作应用ASP.NET MVC全局过滤器 [英] Prevent a ASP.NET MVC global filter from being applied on Elmah action
问题描述
我正在使用Elmah使用 Alex Beletsky的elmah-mvc在我的MVC应用程序中记录异常> NuGet程序包.
I'm using Elmah for logging exceptions on my MVC application using Alex Beletsky's elmah-mvc NuGet package.
应用程序注册了一些全局过滤器,这些全局过滤器应用于每个调用的动作.
The application registers some global filters, applied on each action called.
是否有一种方法可以防止在调用Elmah.Mvc.ElmahController
错误日志页面(foo.com/elmah
)时应用某些过滤器?
Is there a way to prevent some of those filters from being applied when calling the Elmah.Mvc.ElmahController
error log page (foo.com/elmah
) ?
以下测试当然可以,但是我正在寻找一种更优雅的方式,该方式不涉及修改过滤器(也不包括Elmah/Elmah MVC的源代码).甚至有可能吗?
A test like below works, of course, but I'm looking for a more elegant way that would not involve modifying the filter (nor the source code from Elmah / Elmah MVC). Is it even possible ?
public class FooAttribute : FilterAttribute, IActionFilter
{
// ...
public void OnActionExecuting(ActionExecutingContext filterContext)
{
if (filterContext.Controller is ElmahController)
{
return;
}
// do stuff
}
}
-
我知道属性无法在运行时添加或删除.
我想到了将
ElmahController
包装在一个新的包装中,可以在其中添加排除过滤器,但是我不确定如何(如有可能)更改web.config
以引用此包装而不是包装原始控制器.I thought of wrapping the
ElmahController
in a new one where I could add an exclusion filter, but I'm not sure how (if possible) to change theweb.config
to reference this wrapper instead of the original controller.推荐答案
You could register your global filters through a custom
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
中,而不是调用:and in your
Application_Start
instead of calling:FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
您会打电话给
FilterProviders.Providers.Add(new MyFilterProvider());
这篇关于阻止对Elmah操作应用ASP.NET MVC全局过滤器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!