Created
July 13, 2017 01:51
-
-
Save seangwright/8a4d52cd7a60320a53db60c2e2e5456f to your computer and use it in GitHub Desktop.
AspNet Core MVC attribute routing lower/kebab case controller placeholder replacement
This file contains 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
/** | |
Takes something like this | |
[Route("api/customer/{customerId:int}/[controller]")] | |
public class SpecialOrderController : Controller | |
{ | |
// ... | |
[HttpGet("~/api/[controller]")] | |
[Produces(typeof(IEnumerable<SpecialOrderResponse>))] | |
public IActionResult Get() => Ok(db.Query(new GetAllSpecialOrders())); | |
} | |
and with Swashbuckle / Ahoy, generates swagger documentation like | |
GET /api/customer/{customerId}/special-order | |
GET /api/special-order | |
*/ | |
public class KebabRoutingConvention : IControllerModelConvention | |
{ | |
public void Apply(ControllerModel controller) | |
{ | |
foreach (var selector in controller.Selectors) | |
{ | |
selector.AttributeRouteModel = replaceControllerTemplate(selector, controller.ControllerName); | |
} | |
foreach (var selector in controller.Actions.SelectMany(a => a.Selectors)) | |
{ | |
selector.AttributeRouteModel = replaceControllerTemplate(selector, controller.ControllerName); | |
} | |
AttributeRouteModel replaceControllerTemplate(SelectorModel selector, string name) | |
{ | |
return selector.AttributeRouteModel != null | |
? new AttributeRouteModel | |
{ | |
Template = selector.AttributeRouteModel.Template | |
.Replace("[controller]", PascalToKebabCase(name)) | |
} | |
: null; | |
} | |
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(); | |
} | |
} | |
} |
This file contains 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 Startup | |
{ | |
// ... | |
public void ConfigureServices(IServiceCollection services) | |
{ | |
services.AddMvc(options => | |
options.Conventions.Add(new KebabRoutingConvention()) | |
); | |
} | |
// ... | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment