首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >ActionAttribute DI

ActionAttribute DI
EN

Stack Overflow用户
提问于 2016-12-16 12:15:29
回答 1查看 64关注 0票数 1

我不知道如何完成以下工作:

我有一个属性:

代码语言:javascript
复制
public class Authorize : ActionFilterAttribute
  {
      private readonly IAccessPermissionRepository _repository;

      public Authorize(IAccessPermissionRepository repository)
      {
          _repository = repository;
      }
      ...
   }

默认情况下,IAccessPermissionRepository被解析为IoC。

我在控制器类中使用它,像这样

代码语言:javascript
复制
 [ServiceFilter(typeof(Authorize))]
        public IActionResult Index()

但是现在我想向构造函数传递额外的参数,每个操作和控制器都不同。通常我只会使用构造函数。你看。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2016-12-16 14:45:00

您使用TypeFilter而不是ServiceFilter,如下所示:

代码语言:javascript
复制
[TypeFilter(typeof(HasDummyPrivilegeAttribute), Arguments = new object[] { "dummyId" })]

HasDummyPrivilegeAttribute看起来像:

代码语言:javascript
复制
    public class HasDummyPrivilegeAttribute : ActionFilterAttribute
{
    private readonly string _idParameterName;
    private readonly IAccessPermissionRepository _repo;

    public HasServicePrivilegeAttribute(
        string idParameterName,
        [FromServices] IAccessPermissionRepository repo)
    {
        _idParameterName = idParameterName;
        _repo = repo;
    }

    public override void OnActionExecuting(ActionExecutingContext context)
    {
        //TODO Something awesome here with _idParameterName and _repo 
    }
}
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/41184218

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档