简体   繁体   中英

How can I create a “decorator abbreviation” in ASP.net Core

I am working on a project with a lot of models where I need the same validation with localized error messages happen at a couple of different places. Right now I decorate the corresponding properties with

        [RegularExpression(Bla, ErrorMessageResourceType = typeof(Blub), ErrorMessageResourceName = Blablablub)] 
        public int SomeInt { get; set; }

How can I specify a shorthand for this with a custom validation attribute? What I want is something like

        [MyRegularExpressionDecorator] 
        public int SomeInt { get; set; }

Any input is greatly appreciated. Thanks!

public class MyRegularExpressionDecoratorAttribute : RegularExpressionAttribute
{
    public MyRegularExpressionDecoratorAttribute()
        : base("bla") 
    {
        ErrorMessageResourceType = typeof(Blub);
        ErrorMessageResourceName = "Blablablub";
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM