自定义筛选器,用于强制用户填充所有配置文件属性

本文关键字:填充 置文件属性 用户 筛选 用于 自定义 | 更新日期: 2023-09-27 18:30:28

我已经定义了自定义过滤器并添加到全局过滤器以应用于所有操作

public class ProfileRequiredActionFilter : IActionFilter
{
    #region Implementation of IActionFilter
    public void OnActionExecuting(ActionExecutingContext filterContext)
    {
        //Chech that all profile is filled
        filterContext.Result = new RedirectResult("Path-To-Create-A-Profile");
    }
    public void OnActionExecuted(ActionExecutedContext filterContext)
    {
    }
    #endregion
}

asp.net MVC中Profile.GetPropertyValue("name")获取配置文件中属性的值,如果我想检查一个或两个属性它没有问题,如何实现检查配置文件属性是否填充的最佳方法? 我应该使用标志并逐个检查属性stringisemptyornull吗?

自定义筛选器,用于强制用户填充所有配置文件属性

我刚刚添加了返回bool值的新函数,如下所示:

public class ProfileRequiredActionFilter : IActionFilter
{
    public void OnActionExecuting(ActionExecutingContext filterContext)
    {
        public bool checkFilledProfile() {
            return CheckFilledProfile("FirstName", "LastName", "Country", "State", "City", "Address", "PostalCode", "Phone", "Mobile", "Email");
        }
        private static bool CheckFilledProfile(params string[] properties) {
            bool returnValue = true;
            for (int i = 0; i < properties.Length; i++)
                if (string.IsNullOrEmpty(HttpContext.Current.Profile.GetPropertyValue(properties[i]) as string))
                   filterContext.Result = new RedirectResult("Path-To-Create-A-Profile");
            }
    }
}

并在我要检查的操作顶部添加[ProfileRequiredActionFilter]