Last active
July 12, 2019 12:07
-
-
Save xsoheilalizadeh/93f7649f89a60ce64358983662c93594 to your computer and use it in GitHub Desktop.
Strongly typed snake-case [FromQuery] in ASP.NET Core
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| public class CustomFromQueryAttribute : FromQueryAttribute | |
| { | |
| public CustomFromQuery(string name) | |
| { | |
| Name = name.ToSnakeCase(); | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| public static class ObjectExtensions | |
| { | |
| public static string ToSnakeCase(this string o) => Regex.Replace(o, @"(\w)([A-Z])", "$1_$2").ToLower(); | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| public class User | |
| { | |
| [CustomFromQuery(nameof(UserName))] | |
| public string UserName { get; set; } // user_name | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment