简体   繁体   中英

How to get current application url from launchsettings.json

Is there a reasonably simple way to retrieve the current application url from launchsettings.json? For example, in development I have localhost:5000 and production is https://mywebsitesurl.com .

I saw that you can use inject HTTP Context services, but if possible I'd like to access its property in the same way you can access AppSettings.Json properties

in appsettings.json add this

  "AllowedHosts": "*",
  "Services": {
    "NameofMyurlSite": "http://localhost:1000 or your URL site"
  }
}

in startup add this

 services.AddHttpClient("NameofMyurlSite", config => {
        config.BaseAddress = new Uri(Configuration["Services:NameofMyurlSite"]);
    });

if your need use your site for httpclient or receive data, use IHttpClientFactory

 var client = _httpClient.CreateClient("NameofMyurlSite");
                var datasBody = new StringContent(JsonConvert.SerializeObject(clientDatas), Encoding.UTF8, "application/json");
                var response = await client.PostAsync("your-endpoint", datasBody);

example endpoint= https://mywebsitesurl.com/api/datas

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