更新时间:2023-02-15 23:29:36
从ASP.NET RC2开始,[FromServices]已被删除.
As of ASP.NET RC2, [FromServices] has been removed.
如果只想在viewModels中直接为IValidatableObject.Validate提供DI,则可以使用validationContext.GetService(type)来获取服务.这在RC1中不起作用
If you want DI in your viewModels purely for IValidatableObject.Validate then you can use validationContext.GetService(type) to get your service. This did not work in RC1
EG
MyService myService = (MyService)validationContext.GetService(typeof(myService));
这里是一种通用的扩展方法,使它处理起来更加令人愉快.
Here is a generic extension method to make that a little more pleasant to deal with.
public static class ValidationContextExtensions
{
public static T GetService<T>(this ValidationContext validationContext)
{
return (T)validationContext.GetService(typeof(T));
}
}