0

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

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一一检查属性吗?

4

1 回答 1

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]我要检查的操作

于 2013-09-07T18:51:46.643 回答