我在Visual Studio 2015中使用了新的MVC6框架,突然我的所有Data Annotations都停止了工作.所有这些,没有我改变代码. public sealed class RegisterUser{ [Required(ErrorMessage = "required")] [RegularExpression(@"^((.|
public sealed class RegisterUser { [Required(ErrorMessage = "required")] [RegularExpression(@"^((.|\n)*)$", ErrorMessage = "regex")] [StringLength(32, MinimumLength = 3, ErrorMessage = "length")] public string Name { get; set; } ... }
和
[Route(Address + "/membership")] public class MembershipController : Controller { // POST [address]/membership/register [AllowAnonymous] [HttpPost("Register")] public IActionResult Register([FromBody]RegisterUser model) { // Validate the input model. if (model == null) return ... if (!ModelState.IsValid) return ... // Always get HERE } }
为什么我在地球上传递“ModelState.IsValid”测试(它总是评估为真)?
例如,我传递Name =“x”,它仍然评估为true.好像注释不在那里.
它与使用MvcCore有关吗?
令人沮丧的是,我忘记了改为“核心”项目会消除许多常见功能.所以,在Startup.cs中,添加> services.AddMvc()
要么
> services.AddMvcCore().AddDataAnnotations()
取决于您的使用情况.