Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- services.ConfigureRouting(setupAction => {
- setupAction.LowercaseUrls = true;
- });
- [Route("dashboard-settings")]
- class DashboardSettings:Controller {
- public IActionResult Index() {
- // ...
- }
- }
- public class DashedRoutingConvention : IControllerModelConvention
- {
- public void Apply(ControllerModel controller)
- {
- var hasRouteAttributes = controller.Selectors.Any(selector =>
- selector.AttributeRouteModel != null);
- if (hasRouteAttributes)
- {
- // This controller manually defined some routes, so treat this
- // as an override and not apply the convention here.
- return;
- }
- foreach (var controllerAction in controller.Actions)
- {
- foreach (var selector in controllerAction.Selectors.Where(x => x.AttributeRouteModel == null))
- {
- var template = new StringBuilder();
- if (controllerAction.Controller.ControllerName != "Home")
- {
- template.Append(PascalToKebabCase(controller.ControllerName));
- }
- if (controllerAction.ActionName != "Index")
- {
- template.Append("/" + PascalToKebabCase(controllerAction.ActionName));
- }
- selector.AttributeRouteModel = new AttributeRouteModel()
- {
- Template = template.ToString()
- };
- }
- }
- }
- public static string PascalToKebabCase(string value)
- {
- if (string.IsNullOrEmpty(value))
- return value;
- return Regex.Replace(
- value,
- "(?<!^)([A-Z][a-z]|(?<=[a-z])[A-Z])",
- "-$1",
- RegexOptions.Compiled)
- .Trim()
- .ToLower();
- }
- }
- public void ConfigureServices(IServiceCollection services)
- {
- // Add framework services.
- services.AddMvc(options => options.Conventions.Add(new DashedRoutingConvention()));
- }
- foreach (var selector in controllerAction.Selectors
- .Where(x => x.AttributeRouteModel == null))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement