Click here to Skip to main content
15,916,702 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
i have a base url and relative url.
how to combine base url and relative url using app setting config file in asp.net core console application

What I have tried:

i give complete url in app setting file
but now i want break url into 2 parts i.e base url and relative url.
so , how i can achieve this.
Posted
Updated 24-May-21 0:45am
Comments
guptaritu 24-May-21 5:51am    
i tried this:

string baseValue = System.Configuration.ConfigurationManager.AppSettings["Base_GDL_Matltrans"];
string relVaue = System.Configuration.ConfigurationManager.AppSettings["Relative_GDL_Matltrans"];
searchURL = (string)baseValue.Concat(relVaue);

You should use the Uri Class (System) | Microsoft Docs[^] which allows you to build the URI from the base parts.
 
Share this answer
 
Comments
guptaritu 24-May-21 6:29am    
how?
Richard MacCutchan 24-May-21 6:55am    
Read the documentation, where it is all explained.
Use the Uri.TryCreate method[^]:
C#
Uri baseUri;
string baseValue = System.Configuration.ConfigurationManager.AppSettings["Base_GDL_Matltrans"];
if (!Uri.TryCreate(baseValue, UriKind.RelativeOrAbsolute, out baseUri))
{
    throw new ConfigurationErrorsException("Invalid base URL.");
}

Uri searchUri;
string relVaue = System.Configuration.ConfigurationManager.AppSettings["Relative_GDL_Matltrans"];
if (!Uri.TryCreate(baseUri, relValue, out searchUri))
{
    throw new ConfigurationErrorsException("Invalid relative URL.");
}

searchUrl = searchUri.ToString();
If that doesn't work, you'll need to check the values returned from the <appSettings> section in your web.config file.
 
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