Click here to Skip to main content
15,886,518 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a appsetting.json file that is having some api url but in that base url is repeated i want that base url to be separated but i dont want to append base url every time while reading the key from appsetting.json

Is there any mechanism to set the base url in the appsetting.json itself

What I have tried:

"url1": "https://test.com/Account/ForgotUserName",
"url2": "https://test.com/Account/ForgotPassword"


here base url is repeating and i dont want that I want that base url to be seperate

while reading I am injecting IConfiguration and then

var x=_config["url1"]
Posted
Updated 22-Oct-19 7:03am

What I would do would be to define the base url (URL0) as well as the "suffix" portion of the original 2
javasript
"url0": "https://test.com/"
"url1": "Account/ForgotUserName",
"url2": "Account/ForgotPassword"
And then I would create an Extension method to combine and return those URLs
C#
using System;
using System.Text;

public static class ConfigurationExtension {
   public static string GetURL (this IConfiguration configuration, string urlName) {
      StringBuilder url = new StringBuilder()
      try {
         url.Append(configuration["URL0"]);
         url.Append(configuration[urlName]);
      }
      catch (Exception ex) {
         // your error handling
      }
      return url.ToString();
   }
}

And then to access you would simply use
C#
var x=_config.GetURL("url1");
 
Share this answer
 
Comments
Member 12749751 22-Oct-19 13:50pm    
I think this is the better option
MadMyche 22-Oct-19 14:07pm    
Thank you
Is there any issue with appending urls?
var x = _config["BaseUrl"] + _config["url1"] can be a good option where you'll keep "https://test.com/Account/" in BaseUrl key.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900