微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

ASP.NET MVC 3远程验证

我在我的项目路线中有一个验证控制器,我正在使用模型属性中的以下属性一个区域内使用…
[Remote("IsValidUserName","Validation","",ErrorMessage = "Invalid username")]

但是当渲染时,验证是针对与页面相同的区域内的控制器“验证”的“IsValidUserName”操作,而不在根区域内

数据-VAL-远程URL = “/成员/验证/ IsValidUserName”

任何帮助将不胜感激。

谢谢。

解决方法

不幸的是,这个属性是如何实现的。这是这个属性的构造函数的摘录:
public RemoteAttribute(string action,string controller,string areaName) : this()
{
    if (string.IsNullOrWhiteSpace(action))
    {
        throw new ArgumentException(MvcResources.Common_NullOrEmpty,"action");
    }
    if (string.IsNullOrWhiteSpace(controller))
    {
        throw new ArgumentException(MvcResources.Common_NullOrEmpty,"controller");
    }
    this.RouteData["controller"] = controller;
    this.RouteData["action"] = action;
    if (!string.IsNullOrWhiteSpace(areaName))
    {
        this.RouteData["area"] = areaName;
    }
}

请注意IsNullOrWhiteSpace针对areaName进行测试,最终会杀死所有内容

您可以通过编写自定义远程属性来修复它:

public class MyRemoteAttribute : RemoteAttribute
{
    public MyRemoteAttribute(string action,string area)
        : base(action,controller,area)
    {
        this.RouteData["area"] = area;
    }
}

接着:

[MyRemote("IsValidUserName",ErrorMessage = "Invalid username")]
public string Username { get; set; }

现在将生成正确的data-val-remote-url =“/ Validation / IsValidUserName”。

原文地址:https://www.jb51.cc/aspnet/252110.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐