Questions tagged «actionfilterattribute»

3
将依赖项注入ASP.NET MVC 3操作筛选器。这种方法有什么问题?
这是设置。假设我有一些需要服务实例的动作过滤器: 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中,将依赖项注入到动作过滤器中有点麻烦。最常见的方法是使用自定义操作调用因为在这里可以看到:http://www.jeremyskinner.co.uk/2008/11/08/dependency-injection-with-aspnet-mvc-action-filters/的此替代方法的主要动机是因为以下方法被认为与容器不牢固且紧密耦合: public class …


6
为什么我的ASP.NET Web API ActionFilterAttribute OnActionExecuting无法启动?
我正在尝试实施此处看到的内容:http : //www.piotrwalat.net/nhibernate-session-management-in-asp-net-web-api/但我的出现了问题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) …
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.