Advertisement
Guest User

Untitled

a guest
Aug 19th, 2017
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.31 KB | None | 0 0
  1. services.ConfigureRouting(setupAction => {
  2. setupAction.LowercaseUrls = true;
  3. });
  4.  
  5. [Route("dashboard-settings")]
  6. class DashboardSettings:Controller {
  7. public IActionResult Index() {
  8. // ...
  9. }
  10. }
  11.  
  12. public class DashedRoutingConvention : IControllerModelConvention
  13. {
  14.  
  15. public void Apply(ControllerModel controller)
  16. {
  17. var hasRouteAttributes = controller.Selectors.Any(selector =>
  18. selector.AttributeRouteModel != null);
  19. if (hasRouteAttributes)
  20. {
  21. // This controller manually defined some routes, so treat this
  22. // as an override and not apply the convention here.
  23. return;
  24. }
  25.  
  26. foreach (var controllerAction in controller.Actions)
  27. {
  28. foreach (var selector in controllerAction.Selectors.Where(x => x.AttributeRouteModel == null))
  29. {
  30. var template = new StringBuilder();
  31.  
  32. if (controllerAction.Controller.ControllerName != "Home")
  33. {
  34. template.Append(PascalToKebabCase(controller.ControllerName));
  35. }
  36.  
  37.  
  38.  
  39. if (controllerAction.ActionName != "Index")
  40. {
  41. template.Append("/" + PascalToKebabCase(controllerAction.ActionName));
  42. }
  43.  
  44. selector.AttributeRouteModel = new AttributeRouteModel()
  45. {
  46. Template = template.ToString()
  47. };
  48. }
  49. }
  50.  
  51. }
  52.  
  53. public static string PascalToKebabCase(string value)
  54. {
  55. if (string.IsNullOrEmpty(value))
  56. return value;
  57.  
  58. return Regex.Replace(
  59. value,
  60. "(?<!^)([A-Z][a-z]|(?<=[a-z])[A-Z])",
  61. "-$1",
  62. RegexOptions.Compiled)
  63. .Trim()
  64. .ToLower();
  65. }
  66. }
  67.  
  68. public void ConfigureServices(IServiceCollection services)
  69. {
  70. // Add framework services.
  71. services.AddMvc(options => options.Conventions.Add(new DashedRoutingConvention()));
  72. }
  73.  
  74. foreach (var selector in controllerAction.Selectors
  75. .Where(x => x.AttributeRouteModel == null))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement