ASP.NET Core远程验证失效:404错误排查求助
解决ASP.NET Core远程验证返回404未找到的问题
兄弟,我一眼就瞅到你代码里的问题了——你的Remote特性里的控制器名称写错啦!这就是导致验证接口一直返回404的核心原因。
一、修正Remote特性的控制器名称
ASP.NET Core里指定控制器时,不需要加Controller后缀,框架会自动识别控制器类名去掉后缀后的部分作为路由标识。你现在的代码里多写了这个后缀,导致路由匹配失败。
修改后的ViewModel代码:
public class RegisterViewModel { [Remote("IsEmailInUse", "Account")] // 把"AccountController"改成"Account" [Required] [EmailAddress] public string Email { get; set; } [Required] [StringLength(100, ErrorMessage = "The {0} must be at least {2} and at max {1} characters long.", MinimumLength = 6)] [DataType(DataType.Password)] [Display(Name = "Password")] public string Password { get; set; } [DataType(DataType.Password)] [Display(Name = "Confirm password")] [Compare("Password", ErrorMessage = "The password and confirmation password do not match.")] public string ConfirmPassword { get; set; } }
二、确认验证方法的配置没问题
你的IsEmailInUse方法已经正确添加了[AcceptVerbs("GET", "POST")]和[AllowAnonymous]特性,这部分配置是对的——远程验证默认会发起GET请求,这个特性确保两种请求方式都能被处理,不需要调整。
额外检查项(以防万一)
- 确认你的
AccountController没有被自定义路由覆盖,从你提供的代码看用的是默认路由规则,这部分应该没问题; - 你引入的前端验证脚本(jquery、jquery.validate、jquery.validate.unobtrusive)版本兼容,配置也正确,这部分不需要改动。
改完控制器名称后重新运行项目,远程验证应该就能正常触发,不会再返回404了。
内容的提问来源于stack exchange,提问作者JohnK
相关产品推荐
相关产品推荐

