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

查看:15
本文介绍了通过 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),但似乎 Remote 不可用.有谁知道如何使用 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天全站免登陆