在ASP.NET Core中使用远程验证 [英] Using remote validation with ASP.NET Core

查看:220
本文介绍了在ASP.NET Core中使用远程验证的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正尝试如下创建远程验证:

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. See RemoteAttribute in github.
  • After the renaming planned in RC2, it will be in the Microsoft.AspNetCore.Mvc namespace. See RemoteAttribute 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中使用远程验证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆