I'm coding a solution in asp.net core where I need to change the route names putting a slash in the name of controllers and their methods. I'm using this code.
public class SlugifyParameterTransformer : IOutboundParameterTransformer
{
public string TransformOutbound(object value)
{
if (value == null) { return null; }
// Slugify value
return Regex.Replace(
value.ToString(),
"(?<!^)([A-Z][a-z]|(?<=[a-z])[A-Z])",
"-$1",
RegexOptions.Compiled)
.Trim()
.ToLower();
}
}
at Startup.cs
public IServiceProvider ConfigureServices(IServiceCollection services)
{
services.AddMvc(options =>
{
options.Conventions.Add(new RouteTokenTransformerConvention(
new SlugifyParameterTransformer()));
}
);
}
I'll give an example.
The project has two controllers
namespace Selling.API.Controllers
{
[ApiController]
[Route("api/v2/[controller]")]
public class SellsController : ControllerBase
{
[HttpPost("[action]")]
public async Task<ActionResult<IResponse<Sells>>> GetAll(){
// some code here
}
}
}
namespace Selling.API.Controllers
{
[ApiController]
[Route("api/v2/[controller]")]
public class CustomersController : ControllerBase
{
[HttpPost("[action]")]
public async Task<ActionResult<IResponse<Customers>>> GetAll(){
// some code here
}
}
}
If I use the SlugifyParameterTransformer, it will convert routes using slashes, resulting in:
api/v2/sells/get-all
api/v2/customers/get-all
But, I need to ignore sells controller, because some applications use the original format:
api/v2/Sells/GetAll
The code runs perfectly and converts all routes names with slashes, but all controllers are converted and I need to exclude one controller of this convention. I've tried to code some solution in lugifyParameterTransformer class, but without success. How can I solve this?