简体   繁体   中英

Managing dynamic Urls in ASP.NET Core

I am consuming third party APIs in which I fetch base URL from my configuration file (appsettings.json). While creating dynamic endpoints I concatenate URL as follows: $"{BaseUrl}/api/v1/users/{id}/apps"

Now problem is I got feedbacks on this as hardcoding and it is also hard to maintain for future modifications.

Is there any better approach for managing third party APIs endpoint or URLs?

Use static class to manage URLs.

static class ApiRoutes {
private const string Base = "Api"

  Private static Class ControllerOne {
  public static string Get = Base +"\ControllerOne\{id}"
  public static string Post = Base +"\ControllerOne\{id}"
  }

Than In your Controller, You can do something like that:

[httpGet (ApiRoutes.ControllerOne.Get)]
Get (string something) {
  /*your code*/
}

[httpPost(ApiRoutes.ControllerOne.Post)]
Post (string something) {
  /*Your code*/
}

Then, if you will have the desire to change the URL in the future, it will be much easier!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM