简体   繁体   English

在 ASP.NET Core 中管理动态 Url

[英]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).我正在使用第三方 API,在这些 API 中我从我的配置文件 (appsettings.json) 中获取基本 URL。 While creating dynamic endpoints I concatenate URL as follows: $"{BaseUrl}/api/v1/users/{id}/apps"在创建动态端点时,我按如下方式连接 URL: $"{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?有没有更好的方法来管理第三方 API 端点或 URL?

Use static class to manage URLs.使用静态类来管理 URL。

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!那么,如果您以后有更改网址的愿望,那就容易多了!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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