Asp.net mvc Ninject未在ASP.NET MVC中的自定义验证属性中工作

Asp.net mvc Ninject未在ASP.NET MVC中的自定义验证属性中工作,asp.net-mvc,ninject,data-annotations,customvalidator,ninject.web.mvc,Asp.net Mvc,Ninject,Data Annotations,Customvalidator,Ninject.web.mvc,这是一个问题的结果 我正在开发一个ASP.NET MVC Web应用程序。在我的项目中,我使用视图模型类的数据注释进行远程验证。我知道默认远程属性不支持服务器验证。我可以在action方法中再次验证它。但我不想这样做,因为这违反了关注点的分离 因此,我尝试创建自定义服务器客户端远程验证属性。我在网上找到了一个代码并使用了它。但在进行服务器验证时,它给了我一个错误。我正在使用Ninject进行依赖注入。出现错误,因为Ninject无法在验证属性中注入依赖项 这是我的自定义远程验证属性: publi

这是一个问题的结果

我正在开发一个ASP.NET MVC Web应用程序。在我的项目中,我使用视图模型类的数据注释进行远程验证。我知道默认远程属性不支持服务器验证。我可以在action方法中再次验证它。但我不想这样做,因为这违反了关注点的分离

因此,我尝试创建自定义服务器客户端远程验证属性。我在网上找到了一个代码并使用了它。但在进行服务器验证时,它给了我一个错误。我正在使用Ninject进行依赖注入。出现错误,因为Ninject无法在验证属性中注入依赖项

这是我的自定义远程验证属性:

public class RemoteClientServerAttribute : RemoteAttribute
    {
        protected override ValidationResult IsValid(object value, ValidationContext validationContext)
        {
            // Get the controller using reflection
            Type controller = Assembly.GetExecutingAssembly().GetTypes()
                .FirstOrDefault(type => type.Name.ToLower() == string.Format("{0}Controller",
                    this.RouteData["controller"].ToString()).ToLower());
            if (controller != null)
            {
                // Get the action method that has validation logic
                MethodInfo action = controller.GetMethods()
                    .FirstOrDefault(method => method.Name.ToLower() ==
                        this.RouteData["action"].ToString().ToLower());
                if (action != null)
                {
                    // Create an instance of the controller class
                    object instance = Activator.CreateInstance(controller);
                    // Invoke the action method that has validation logic
                    object response = action.Invoke(instance, new object[] { value });
                    if (response is JsonResult)
                    {
                        object jsonData = ((JsonResult)response).Data;
                        if (jsonData is bool)
                        {
                            return (bool)jsonData ? ValidationResult.Success :
                                new ValidationResult(this.ErrorMessage);
                        }
                    }
                }
            }

            return ValidationResult.Success;
            // If you want the validation to fail, create an instance of ValidationResult
            // return new ValidationResult(base.ErrorMessageString);
        }

        public RemoteClientServerAttribute(string routeName)
            : base(routeName)
        {
        }

        public RemoteClientServerAttribute(string action, string controller)
            : base(action, controller)
        {
        }

        public RemoteClientServerAttribute(string action, string controller,
            string areaName)
            : base(action, controller, areaName)
        {
        }
    }
这是我的控制器类

public class CategoryController : Controller
    {
        private ICategoryRepo categoryRepo;

        public CategoryController()
        {

        }

        public CategoryController(ICategoryRepo categoryParam)
        {
            this.categoryRepo = categoryParam;
        }
        .
        .
        //remote validation action
         public JsonResult IsNameUnique(string Name)
        {
            IEnumerable<Category> categories = categoryRepo.Categories.Where(x => x.Name.Trim() == Name);

            Category category = categories.FirstOrDefault();
            return Json(category==null, JsonRequestBehavior.AllowGet);
        }
}
但问题是,如果我这样做,我使用Ninject的原因就毫无意义了。它正在制造依赖。但如果我不这样做,CategoryReporto将在IsNameUnique操作中抛出null异常。那么,如何使Ninject在自定义远程验证属性中工作?

尝试替换:

object instance = Activator.CreateInstance(controller);


我必须指出,这是对有争议的模式的使用。但是我几乎看不出如何在这个属性中做得有什么不同。

不使用Activator.CreateInstance创建控制器,您能让NInject将它提供给您吗?我对NInject了解不够。过滤器呢?我认为在过滤器的构造函数上设置DI比在属性上设置DI更容易,在这种情况下也可以实现同样的功能。谢谢。它工作得很好。非常感谢你的帮助。
object instance = Activator.CreateInstance(controller);
object instance = DependencyResolver.Current.GetService(controller);