The [Route] attribute implements a IRouteTemplateProvider interface, which defines a Template property that returns the route template.
If you implement a custom attribute, say [DynamicRoute], that takes an environment variable name as a parameter, you could set the Template property to a different value based on the environment variable passed in.
This works for me:
[AttributeUsage(AttributeTargets.Class | AttributeTargets.Method, AllowMultiple = true, Inherited = true)]
public sealed class DynamicRouteAttribute : Attribute, IRouteTemplateProvider
{
public DynamicRouteAttribute(string environmentVariableName)
{
ArgumentNullException.ThrowIfNull(environmentVariableName);
Template = Environment.GetEnvironmentVariable(environmentVariableName);
}
public string? Template { get; }
public int? Order { get; set; }
public string? Name { get; set; }
}
Replace [Route("[controller]")] in your controller with [DynamicRoute("yourEnvVarName")] and it will start to honor whatever you configure in your environment variable.