问题描述
我正尝试如下创建远程验证:
I am trying to create a remote validation as follows:
[Remote("CheckValue", "Validate", ErrorMessage="Value is not valid")]
public string Value { get; set; }
我正在使用ASP.NET Core(ASP.NET 5),并且似乎远程"不可用.有谁知道如何使用ASP.NET CORE做到这一点?
I am using ASP.NET Core (ASP.NET 5) and it seems Remote is not available.Does anyone know how to do this with ASP.NET CORE?
推荐答案
RemoteAttribute是ASP.Net MVC Core的一部分:
The RemoteAttribute is part of ASP.Net MVC Core:
- 如果使用的是RC1,它位于
Microsoft.AspNet.Mvc
命名空间中.请参阅中的RemoteAttribute
github. - 在RC2中计划重命名后,它将在
Microsoft.AspNetCore.Mvc
名称空间中.请参阅中的RemoteAttribute
github.
- If you are using RC1, it is in the
Microsoft.AspNet.Mvc
namespace. SeeRemoteAttribute
in github. - After the renaming planned in RC2, it will be in the
Microsoft.AspNetCore.Mvc
namespace. SeeRemoteAttribute
in github.
例如,在RC1中,创建一个带有身份验证的新MVC站点.然后使用在本地控制器中调用方法的虚拟远程验证更新生成的LoginViewModel
:
For example, in RC1 create a new MVC site with authentication. Then update the generated LoginViewModel
with a dummy remote validation calling a method in the home controller:
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; }
...
}
如果您在家庭控制器中创建该虚拟方法并设置一个断点,则只要在登录表单中更改电子邮件,就会看到该断点被击中:
If you create that dummy method in the home controller and set a breakpoint, you will see it is hit whenever you change the email in the login form:
public class HomeController : Controller
{
...
public bool Foo()
{
return false;
}
}
这篇关于在ASP.NET Core中使用远程验证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!