Search code examples
c#asp.net-web-api.net-coreurl-routingasp.net-web-api-routing

Include placeholder in API base path .net core


Wondering how you would go about including a placeholder/variable in the API base path, you would see something like this used in azure devops for example: https://dev.azure.com/{company-name}/. How would you set up your API to have context of the start of the base path.

What I would like to achieve is to let my API have context of which account the API calls are executed in without it having to be part of the API request body. The format of my API would be https://some-company.com/{account}/_apis/some-api/controller/.


Solution

  • You cannot do it the suggested way, routing parameters only apply for the route of your controller.

    But you could try to extract the information from the current context:

    HttpContext.Current.Request.Url.AbsoluteUri