我正在将FluentValidation与MVC 5.1.0.0集成。并尝试使用StructureMap但我在编译代码时遇到错误:
错误5程序集'FluentValidation.Mvc,Version = 5.4.0.0,Culture = neutral,PublicKeyToken = null'使用'System.Web.Mvc,Version = 5.2.0.0,Culture = neutral,PublicKeyToken = 31bf3856ad364e35'高于引用程序集的版本'System.Web.Mvc,Version = 5.1.0.0,Culture = neutral,PublicKeyToken = 31bf3856ad364e35'c:\ Users \ XXXX \ Documents \ Visual Studio 2013 \ Projects \ XXXX \ packages \ FluentValidation.MVC5.5.4 .0.0 \ lib \ Net45 \ FluentValidation.Mvc.dll XXX.Web
如何解决此错误?我试图安装不同版本的StructureMap,但它不起作用。在我的Globals.asax.cs中,我添加了以下代码:
//Configure structuremap
ObjectFactory.Configure(cfg => cfg.AddRegistry(new MyRegistry()));
ControllerBuilder.Current.SetControllerFactory(new StructureMapControllerFactory());
//Configure FV to use StructureMap
var factory = new StructureMapValidatorFactory();
//Tell MVC to use FV for validation
ModelValidatorProviders.Providers.Add(new FluentValidationModelValidatorProvider(factory));
DataAnnotationsModelValidatorProvider.AddImplicitRequiredAttributeForValueTypes = false;
我也有这个课程:
using System.Web.Mvc;
using FluentValidation;
using FluentValidationIoC.Controllers;
using FluentValidationIoC.Models;
using StructureMap.Configuration.DSL;
public class MyRegistry : Registry
{
public MyRegistry()
{
For<PersonRepository>().Use<PersonRepository>();
For<HomeController>().Use<HomeController>();
For<IValidator<Person>>()
.Singleton()
.Use<PersonValidator>();
}
}
和
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using StructureMap;
namespace Movie.Web.StructureMap
{
using System;
using FluentValidation;
using StructureMap;
public class StructureMapValidatorFactory : ValidatorFactoryBase
{
public override IValidator CreateInstance(Type validatorType)
{
return ObjectFactory.TryGetInstance(validatorType) as IValidator;
}
}
}
有谁能告诉我如何解决这个问题?