自定义过滤器强制用户填写所有配置文件属性

时间:2013-09-07 18:03:29

标签: c# asp.net asp.net-mvc

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

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吗?

1 个答案:

答案 0 :(得分:0)

我刚刚添加了返回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]