根据收到的值将值传递给属性

时间:2016-05-12 13:47:53

标签: c# asp.net-web-api validationattribute

我正在使用C#4.5 web api类型项目。

当从客户端收到HttpPost请求时,我想验证请求字段。 问题是输入验证取决于输入本身,或者更具体,取决于提供的枚举。

假设我有以下课程:

public class A 
{
     public myEnum Filter{set;get;}

     [myEnumCheckName(Filter)]
     public string Name {set;get;}

     [myEnumCheckEmail(Filter)]
     public string Email {set;get;}
}

public enum myEnum {ByName = 1, ByEmail = 2}

我还写了2个validatorAttributes:

[AttributeUsage(AttributeTargets.Property | AttributeTargets.Field)]
public class myEnumCheckName : ValidationAttribute
{
    private readonly someNameValidator _validator = new someNameValidator();
    private readonly myEnum _filter;

    public myEnumCheckName(myEnum filter)
    {
        _filter = filter;
    }

    public override bool IsValid(object value)
    {
        if (_filter == myEnum.ByName)
            return _validator.IsValid(value);

        return true;
    }
}

[AttributeUsage(AttributeTargets.Property | AttributeTargets.Field)]
public class myEnumCheckEmail : ValidationAttribute
{
    private readonly someEmailValidator _validator = new someEmailValidator();
    private readonly myEnum _filter;

    public myEnumCheckEmail(myEnum filter)
    {
        _filter = filter;
    }

    public override bool IsValid(object value)
    {
        if (_filter == myEnum.ByEmail)
            return _validator.IsValid(value);

        return true;
    }
}

问题是,当我尝试拨打[myEnumCheckName(myEnum)时,它会说:

  

无法在静态上下文中访问静态属性myEnum。

无论如何都要访问实例/当前上下文字段值?

感谢。

澄清

我想验证NameEmail属性,这就是一般的想法。 现在,我不介意Name如果Filter值为ByEmail则为set tabstop=4 set sts=4 set expandtab set softtabstop=4 set shiftwidth=4 ,反之亦然。 这样,我就不必编写两次处理请求的方法了。

1 个答案:

答案 0 :(得分:1)

使用A中的内置验证框架无法实现此类验证逻辑。如果要根据其他属性值自动验证类属性,则应切换到具有此类功能的其他验证框架。

我建议您查看FluentValidation,并为您的类创建自定义验证器,以便轻松处理您所描述的复杂情况。例如,您可以为public class AValidator: AbstractValidator<A> { public AValidator() { RuleFor(a => a.Name) .NotEmpty() .When(c => c.Filter == myEnum.ByName); RuleFor(a => a.Email) .NotEmpty() .When(c => c.Filter == myEnum.ByEmail); } } 类创建一个类似于以下类的验证器:

FluentValidation

要了解有关如何将public class MainActivity extends Activity { /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); // load the fragment - we only need to do this when the activity is first created // - not on a recreation due to screen rotation, etc if (savedInstanceState == null) { FragmentManager fm = this.getFragmentManager(); FragmentTransaction ft = fm.beginTransaction(); ft.add(R.id.container, new WebViewFragment()); ft.commit(); } } // this could go in a separate file public static class WebViewFragment extends Fragment { WebView mWebView; @Override public View onCreateView(LayoutInflater layoutInflater, ViewGroup viewGroup, Bundle bundle) { View v = layoutInflater.inflate(R.layout.webviewfrag, null, false); this.mWebView = (WebView)v.findViewById(R.id.NewsFeedWbview); this.mWebView.loadUrl("https://google.com"); return v; } } } 集成到Web Api中的更多信息,您可以参考以下教程(如果您不想通过依赖项注入框架提供验证器,则可以忽略Ninject部分):
http://sergeyakopov.com/restful-validation-with-asp-net-web-api-and-fluentvalidation/