Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

What is the idiomatic way to add custom validation to Asp.Net Core controller parameters

In my api, I have a controller that takes a string as an argument.

[HttpPost("{text}")]
public ActionResult PostSomething(string text){}

I want text to be one of three valid strings, lets say "a", "b", "c". Is there a way to do this using some attribute or is the best way to simply have an if statment inside the controller?

Thank you in advance.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

In your simple scenario yes you can use attribute but it is better to follow Separation of Concerns principle and Single Responsibility Principle and extract all your validation logic into separate classes Validators in this will help using such framework as Fluent Validation

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading