Tag: actionfilterattribute

为什么我的ASP.NET Web API ActionFilterAttribute的OnActionExecuting不能触发?

我试图实现什么在这里看到: http : NhSessionManagementAttribute但我有我的NhSessionManagementAttribute问题。 我在我的OnActionExecuting(HttpActionContext actionContext)上设置了断点,以查看该函数是否曾经被调用 – 事实并非如此。 我仔细检查了我的global.asax.cs文件,发现我实际上注册了ActionFilter : GlobalConfiguration.Configuration.Filters.Add(new NhSessionManagementAttribute()); 我也装饰了我的控制器类本身,以及它的行为与属性无济于事: public class ClientsController : ApiController { static readonly ClientRepository repository = new ClientRepository(); [NhSessionManagement] public IEnumerable<Client> GetAllClients() { return repository.GetAll(); } [NhSessionManagement] public Client GetClient(int id) { Client client = repository.Get(id); if (client == null) { throw new HttpResponseException( new HttpResponseMessage(HttpStatusCode.NotFound) ); […]

将dependency injection到ASP.NET MVC 3操作filter中。 这种方法有什么问题?

这是设置。 假设我有一些需要服务实例的动作filter: public interface IMyService { void DoSomething(); } public class MyService : IMyService { public void DoSomething(){} } 然后我有一个ActionFilter需要一个该服务的实例: public class MyActionFilter : ActionFilterAttribute { private IMyService _myService; // <— How do we get this injected public override void OnActionExecuting(ActionExecutingContext filterContext) { _myService.DoSomething(); base.OnActionExecuting(filterContext); } } 在MVC 1/2注入依赖到行动filter是屁股有点痛苦。 最常见的方法是使用自定义操作调用程序,可以在这里看到: http : //www.jeremyskinner.co.uk/2008/11/08/dependency-injection-with-aspnet-mvc-action-filters/这个解决方法背后的主要动机是因为下面的方法被认为是容易和紧密耦合的容器: public class […]

如何有select地禁用ASP.Net MVC中的全局filter

我已经设置了一个全局filter,用于打开和closuresNHibernate会话的所有控制器操作。 95%的这些行动需要一些数据库访问,但是5%不需要。 有没有简单的方法来禁用这5%的全球filter。 我可以倒过来装饰只需要数据库的行动,但这将是更多的工作。