自定义验证:从validationContext获取属性名称

发布于 2024-12-14 09:20:22 字数 1507 浏览 2 评论 0 原文

对于我的 ASP.NET MVC 项目,我创建了一个自定义验证属性。这是我正在努力解决的代码:

  protected override ValidationResult IsValid(object value, ValidationContext validationContext) {

        //Here I need to resolve the url in order to make a call to that controller action and get the JSON result back

        var httpContext = new HttpContextWrapper(HttpContext.Current);
        var urlHelper = new UrlHelper(
            new System.Web.Routing.RequestContext(
                httpContext, new System.Web.Routing.RouteData()
            )
        );
        var url = urlHelper.Action(Action, Controller, null, 
            urlHelper.RequestContext.HttpContext.Request.Url.Scheme);

        var fullUrl = string.Format("{0}?{1}={2}", url, 
            /*validationContext.MemberName*/"term", value);

        if (!GetResult(fullUrl)) {

            var message = FormatErrorMessage(validationContext.DisplayName);
            return new ValidationResult(message);
        }

        return null;
    }

您可以从下面的链接看到完整的代码:

https://bitbucket.org/tugberk/tugberkug.mvc/src/6cc3d3d64721/TugberkUg.MVC/Validation/ServerSideRemoteAttribute.cs

对于 fullUrl 变量,我我正在尝试将属性名称附加到查询字符串,但是当我使用 validationContext.MemberName 时,我失败。我通过将其静态化为“术语”来解决临时修复的问题,但这根本不是修复。

那么,从 validationContext 中检索属性名称的方法是什么?

For my ASP.NET MVC projects, I created a custom validation attribute. Here is the code I am struggling with :

  protected override ValidationResult IsValid(object value, ValidationContext validationContext) {

        //Here I need to resolve the url in order to make a call to that controller action and get the JSON result back

        var httpContext = new HttpContextWrapper(HttpContext.Current);
        var urlHelper = new UrlHelper(
            new System.Web.Routing.RequestContext(
                httpContext, new System.Web.Routing.RouteData()
            )
        );
        var url = urlHelper.Action(Action, Controller, null, 
            urlHelper.RequestContext.HttpContext.Request.Url.Scheme);

        var fullUrl = string.Format("{0}?{1}={2}", url, 
            /*validationContext.MemberName*/"term", value);

        if (!GetResult(fullUrl)) {

            var message = FormatErrorMessage(validationContext.DisplayName);
            return new ValidationResult(message);
        }

        return null;
    }

You can see the full code from below link :

https://bitbucket.org/tugberk/tugberkug.mvc/src/6cc3d3d64721/TugberkUg.MVC/Validation/ServerSideRemoteAttribute.cs

For the fullUrl variable, I am trying to append the property name to querystring but when I use validationContext.MemberName, I am failing. I solved the problem with a temp fix by making it static as "term" but it is not a fix at all.

So, what is the way of retrieving property name from validationContext?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

很快妥协 2024-12-21 09:20:22

validationContext.DisplayName 可以解决问题吗?

然后您可以思考是否可以获取 MemberName

var displayName = validationContext.DisplayName;

var memberName = validationContext.ObjectType.GetProperties()
    .Where(p => p.GetCustomAttributes(false).OfType<DisplayAttribute>().Any(a => a.Name == displayName))
    .Select(p => p.Name)
    .FirstOrDefault();

Does validationContext.DisplayName do the trick?

You could then reflect to get the MemberName

var displayName = validationContext.DisplayName;

var memberName = validationContext.ObjectType.GetProperties()
    .Where(p => p.GetCustomAttributes(false).OfType<DisplayAttribute>().Any(a => a.Name == displayName))
    .Select(p => p.Name)
    .FirstOrDefault();

Possibly?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文