Skip to content

Instantly share code, notes, and snippets.

@xsoheilalizadeh
Last active July 12, 2019 12:07
Show Gist options
  • Select an option

  • Save xsoheilalizadeh/93f7649f89a60ce64358983662c93594 to your computer and use it in GitHub Desktop.

Select an option

Save xsoheilalizadeh/93f7649f89a60ce64358983662c93594 to your computer and use it in GitHub Desktop.
Strongly typed snake-case [FromQuery] in ASP.NET Core
public class CustomFromQueryAttribute : FromQueryAttribute
{
public CustomFromQuery(string name)
{
Name = name.ToSnakeCase();
}
}
public static class ObjectExtensions
{
public static string ToSnakeCase(this string o) => Regex.Replace(o, @"(\w)([A-Z])", "$1_$2").ToLower();
}
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