Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Dynamically Change Routing in ASP.NET 6.0 Web API

I want to take an environmental variable and prefix it to the route for all the controllers. So in Controllers I have the followings:

[ApiController]
[Route("[controller]")]
public class DashboardsController : ControllerBase
{.......}

Program.cs routing part ==>

app.UseRouting();
app.UseAuthorization();
app.MapControllers();
app.Run();

I have tried creating a class with constant strings and putting into the Route attribute but as you know we cannot change constants, so this didn't work.

like image 539
VahidDev Avatar asked Feb 21 '26 02:02

VahidDev


1 Answers

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.

like image 197
julealgon Avatar answered Feb 23 '26 15:02

julealgon



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!