小编Dev*_*zzz的帖子

使用StructureMap的Asp.Net MVC 5自定义操作过滤器

我在asp.net mvc自定义acitonfilte中遇到问题使用我的"LogAttribute"类中的structuremap我有setter依赖注入,当执行我的customfilterclass的"OnActionExecuted"方法时,它是null,这是"LogAttribute"

我的LogAttribute类代码是

    public class LogAttribute : ActionFilterAttribute
{
    public ApplicationDbContext Context { get; set; }
    public string Description { get; set; }
    public LogAttribute(string description)
    {
        Description = description;
    }

    public override void OnActionExecuted(ActionExecutedContext filterContext)
    {
        var userId = filterContext.HttpContext.User.Identity.GetUserId();
        var user = Context.Users.Find(userId); **i am getting error here the Context is coming null here** 
        Context.Logs.Add(new Log(user, filterContext.ActionDescriptor.ActionName,
                                filterContext.ActionDescriptor.ControllerDescriptor.ControllerName,
                                Description
                                )
                        );
        Context.SaveChanges();
    }
}
Run Code Online (Sandbox Code Playgroud)

我创建了另一个类,我将值传递给setter依赖项属性

    public class StructureMapFilterProvider : FilterAttributeFilterProvider 
{
    private readonly Func<IContainer> _container;
    public StructureMapFilterProvider(Func<IContainer> container) …
Run Code Online (Sandbox Code Playgroud)

asp.net asp.net-mvc custom-action-filter

4
推荐指数
2
解决办法
3446
查看次数