Asp.net core 使用ASP.NET核心的远程验证
我正在尝试创建远程验证,如下所示:Asp.net core 使用ASP.NET核心的远程验证,asp.net-core,asp.net-core-mvc,Asp.net Core,Asp.net Core Mvc,我正在尝试创建远程验证,如下所示: [Remote("CheckValue", "Validate", ErrorMessage="Value is not valid")] public string Value { get; set; } 我使用的是ASP.NET核心(ASP.NET 5),似乎远程不可用。 有人知道如何使用ASP.NET CORE执行此操作吗?RemoteAttribute是ASP.NET MVC CORE的一部分: 如果您使用的是RC1,则它位于Microsoft.A
[Remote("CheckValue", "Validate", ErrorMessage="Value is not valid")]
public string Value { get; set; }
我使用的是ASP.NET核心(ASP.NET 5),似乎远程不可用。
有人知道如何使用ASP.NET CORE执行此操作吗?RemoteAttribute是ASP.NET MVC CORE的一部分:
- 如果您使用的是RC1,则它位于
命名空间中。请参阅github中的Microsoft.AspNet.Mvc
- 在RC2中计划的重命名之后,它将位于
命名空间中。请参阅github中的Microsoft.AspNetCore.Mvc
LoginViewModel
:
using Microsoft.AspNet.Mvc;
using System.ComponentModel.DataAnnotations;
public class LoginViewModel
{
[Required]
[EmailAddress]
[Remote("Foo", "Home", ErrorMessage = "Remote validation is working")]
public string Email { get; set; }
...
}
如果在主控制器中创建该虚拟方法并设置断点,则无论何时更改登录表单中的电子邮件,都会看到该方法被命中:
public class HomeController : Controller
{
...
public bool Foo()
{
return false;
}
}
同时,文件已经存在并可用。简而言之,您只需要使用以下内容装饰视图模型的属性:
[Remote(action: "Foo", controller: "Bar", ErrorMessage = "Remote validation is working")]
[Required]
[Display(Name = "Name")]
public string Name { get; set; }
然后在控制器(本例中名为“Bar”)中创建一个操作(本例中名为“Foo”),并在其中添加逻辑:
[AcceptVerbs("Get", "Post")]
public async Task<IActionResult> Foo(string name)
{
bool exists = await this.Service.Exists(name);
if (exists)
return Json(data: false);
else
return Json(data: true);
}
[AcceptVerbs(“Get”、“Post”)]
公共异步任务Foo(字符串名称)
{
bool exists=等待this.Service.exists(名称);
如果(存在)
返回Json(数据:false);
其他的
返回Json(数据:true);
}
最后一点注意:远程属性位于
Microsoft.AspNetCore.Mvc
命名空间中。你说得对。。。我在查看System.Component.DataAnnotations,甚至在Github中找到了该库的源代码。当然是在Microsoft.AspNet.Mvc中。谢谢链接断了