如何在ASP.NET MVC中的自定义验证属性中检索formcollection值或路由数据

问题描述 投票:1回答:1

我正在开发一个Asp.NET MVC5项目。在我的项目中,我正在创建一个自定义验证属性。但是我在从自定义属性中检索已发布的表单数据时遇到问题。我没有检索ViewModel的属性值。我只需要检索formcollection值或实际的数据值。

我有这样的动作

public ActionResult postData(ViewModel model, string routeDta)
{
   //do action here
}

这是我的自定义属性,需要检索路线数据

public class RemoteClientServerAttribute : RemoteAttribute
    {
        protected override ValidationResult IsValid(object value, ValidationContext validationContext)
        {
            //I want to retrieve routeDta from action above here
            //It is also ok if I can retrieve form collection value
        }

        public RemoteClientServerAttribute(string routeName)
            : base(routeName)
        {
        }

        public RemoteClientServerAttribute(string action, string controller)
            : base(action, controller)
        {
        }

        public RemoteClientServerAttribute(string action, string controller,
            string areaName)
            : base(action, controller, areaName)
        {
        }
    }

我评论了要在哪里检索数据。如何从自定义属性检索表单集合值或路由数据?我不尝试从ViewModel检索属性值。

asp.net-mvc data-annotations model-binding validationattribute
1个回答
0
投票

var httpContextAccessor =(IHttpContextAccessor)validationContext.GetService(typeof(IHttpContextAccessor));;var user = httpContextAccessor.HttpContext.User;

© www.soinside.com 2019 - 2024. All rights reserved.