0

How can I replace custom placeholder before each route in .Net Core 2.1

For Example {serverkey}

[Route("api/{serverkey}/[controller]")]
    [ApiController]
    [AuthorizationFilter]
    public class MonitoringController : Controller
    {
        // Code...
    }

I want each url to be like api/key/Monitoring/Action. So I need to replace key in each request/route.

So basically is there any way to replace the placeholder {serverkey} or something like this {serverkey:key} or even [serverkey] before each request map to this controller.

I know there could be a way like [controller] is replaced automatically by .net core but I am not able to find it.

Thanks in advance.

Voodoo
  • 1,550
  • 1
  • 9
  • 19
  • Where is `{serverKey}` value coming from? – Jamie Rees Feb 28 '20 at 08:49
  • What do you mean by replaced? Putting a `[Route("api/{serverkey}/[controller]")]` attribute on top of your controller, and accessing the value in your action methods (like this `[HttpGet] public string Get(string serverkey)`) isn't enough? – Bruno Martins Feb 28 '20 at 08:50
  • I have added `serverKey` in appsetting.json. But i need to find a way to replace route placeholder with this key. – Voodoo Feb 28 '20 at 08:50
  • @Voodoo What happens is the `serverkey` provided in the url doesn't match the one in the appsettings file? I think you could deal with that with a custom `AuthorizeAttribute` – Bruno Martins Feb 28 '20 at 08:52
  • @bmartins I don't want to get the key from input, but I want my route to be replaced by my own key stored. – Voodoo Feb 28 '20 at 08:52
  • @bmartins See I don't want to pass key at all, but I want my route to be modified according to the key so that it can pass the custom rules written in nginx on server. – Voodoo Feb 28 '20 at 08:53
  • @Voodoo What do you mean by "I don't want to pass the key"? Consider that the `serverKey` is `xxx` in the `appsettings.json`. What happens if a user calls the endpoint `api/xxx/monitoring/action`? What happens for the endpoint `api/yyy/monitoring/action`? – Bruno Martins Feb 28 '20 at 09:02
  • @bmartins It will simply return 404 – Voodoo Feb 28 '20 at 09:07
  • Are you thnking of route constraints? https://learn.microsoft.com/en-us/aspnet/core/fundamentals/routing?view=aspnetcore-3.1#custom-route-constraints – Mark Davies Feb 28 '20 at 09:17
  • @MarkDavies No. Constraint will check whether the key is valid or not. Am i right? But i want my url to be replaced by actual key, so that the swagger will generate urls by prepending the key. Is this making sense? – Voodoo Feb 28 '20 at 09:20
  • @Voodoo If only one `serverKey` value is possible, why do you want to pass it in the url? Is it a clumsy workaround to avoid setting up an authentication mechanism? – Bruno Martins Feb 28 '20 at 09:21
  • I can't. Actually we have API service which we are injecting it into the other services to reuse. So we want to prep-end the key to pass the custom server rules written in nginx. So the key will change based on the service which are using our common service. – Voodoo Feb 28 '20 at 09:23

1 Answers1

0

I am able to create custom route using IRouteTemplateProvider

public class CustomRouteAttribute : Attribute, IRouteTemplateProvider
    {
        string serverKey = string.Empty;

        public CustomRouteAttribute()
        {
            string serverKey = "Your Key";
        }

        public string Template => $"api/{serverKey}/[controller]";

        public int? Order { get; set; }

        public string Name { get; set; }
    }

It will prepend your key before each route.

Usage: Use this as a attribute on your controller

//[Route("api/[controller]")]
    [CustomRoute]
    [ApiController]
    public class DemoController : ControllerBase
    {}

For Example:

http://localhost:52264/api/Controller/Action will becomes http://localhost:52264/api/Yourkey/Controller/Action

Voodoo
  • 1,550
  • 1
  • 9
  • 19
  • I am also interested to implement something similar. How would your full code sample look like, if the serverKey would be read from a json settings file? How could this be injected into the CustomRouteAttribute class? – RickyTad Nov 16 '22 at 11:20
  • You can check these links https://stackoverflow.com/questions/4102138/how-to-use-dependency-injection-with-an-attribute and https://stackoverflow.com/questions/29915192/dependency-injection-in-attributes – Voodoo Apr 18 '23 at 07:42