这个问题是 this question 的结果.
我正在开发 ASP.NET MVC Web 应用程序。在我的项目中,我正在使用 View 模型类的数据注释进行远程验证。我知道默认远程属性不支持服务器验证。我可以在操作方法中再次验证它。但我不想这样做,因为它违反了关注点分离。
所以我尝试创建自定义服务器客户端远程验证属性。我在网上找到了一个代码并使用了它。但是当服务器验证发生时它给了我错误。我正在使用 Ninject 进行依赖注入(inject)。发生错误是因为 Ninject 无法在验证属性中注入(inject)依赖项。
这是我的自定义远程验证属性:
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)
{
}
}
这是我的 Controller 类
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);
}
}
当验证通过客户端并到达服务器端时,它开始抛出错误。
这是错误
是的,它抛出 no method found 异常,因为它找不到没有参数的构造函数。
我添加了这样的无参数构造函数
public CategoryController()
{
categoryRepo = new CategoryRepo();
}
但问题是,如果我这样做,我使用 Ninject 的原因就没有任何意义了。它正在产生依赖性。但如果我不这样做,categoryRepo 将在 IsNameUnique 操作中抛出 null 异常。那么如何让 Ninject 在我的自定义远程验证属性中工作呢?
最佳答案
尝试替换:
object instance = Activator.CreateInstance(controller);
与
object instance = DependencyResolver.Current.GetService(controller);
我必须指出,这是有争议的 ServiceLocator
的用法。图案。但我几乎不知道如何在此属性中以不同的方式进行操作。
关于asp.net-mvc - Ninject 无法在 ASP.NET MVC 中的自定义验证属性中工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37947539/