Now I need to add {ServerName=”server-name; Location=”location”; Owner=”owner”} to that URL and then execute that link.
The server name, location and owner data is coming from a SQL Server DB. I need to query the DB and populate the URL from the results and I need to loop that task based on the number of rows in the query results.
You can use the UriBuilder.Query property to set any query information contained in the URI.
I wrote a simple example, you can refer to it.
Model
public class testdata1
{
public string ServerName { get; set; }
public string Location { get; set; }
public string Owner { get; set; }
}
Index
public async Task<ActionResult> Index()
{
//Assuming that data has been queried from the database //I assumed some data.
List<testdata1> testlist = new List<testdata1>();
for(int i =0; i < 2; i++)
{
testlist.Add(new testdata1 {
ServerName = "server"+i,
Location = "location"+i,
Owner = "owner"+i
});
}
for (int i = 0; i < testlist.Count; i++)
{
UriBuilder baseUri = new UriBuilder("https://localhost:44307/api/Values/test1");//Create query string
//Use the UriBuilder.Query property to get or set all the query information contained in the URI.
string queryToAppend = "ServerName=" + testlist[i].ServerName + "&Location=" + testlist[i].Location + "&Owner=" + testlist[i].Owner;
if (baseUri.Query != null && baseUri.Query.Length > 1)
{
baseUri.Query = baseUri.Query.Substring(1) + "&" + queryToAppend;
}
else
{
baseUri.Query = queryToAppend;
}
var client = new HttpClient();
client.DefaultRequestHeaders.Accept.Clear();
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
HttpResponseMessage response = await client.GetAsync(baseUri.ToString());
if (response.IsSuccessStatusCode)
{
}
}
return View();
}
ASP.NET forums are moving to a new home on Microsoft Q&A, we encourage you to go to Microsoft Q&A for .NET for posting new questions and get involved today. Learn more >
None
0 Points
3 Posts
How to Dynamically Append to a Base URL using HttpClient
Aug 21, 2020 08:09 PM|ronniej962|LINK
Is it possible to dynamically append to an URL from SQL query results using HttpClient class in C#?
I inherited an ASP.Net webapp that I need to updated
I have a base URL like – https://SomeSite.com/api/
Now I need to add {ServerName=”server-name; Location=”location”; Owner=”owner”} to that URL and then execute that link.
The server name, location and owner data is coming from a SQL Server DB. I need to query the DB and populate the URL from the results and I need to loop that task based on the number of rows in the query results.
So the URL should look like - http://SomeSite.com/api/{ServerName=MyServer; Location=MyLocation; Owner=Me}
I’ve been unable to find any examples on how to append to the URL.
Thanks in advance!
All-Star
58474 Points
15800 Posts
Re: How to Dynamically Append to a Base URL using HttpClient
Aug 22, 2020 03:10 PM|bruce (sqlwork.com)|LINK
See the query string property.
https://docs.microsoft.com/en-us/dotnet/api/system.net.webclient.querystring?view=netstandard-2.0
Contributor
3070 Points
868 Posts
Re: How to Dynamically Append to a Base URL using HttpClient
Aug 24, 2020 06:28 AM|YihuiSun|LINK
Hi ronniej962,
You can use the UriBuilder.Query property to set any query information contained in the URI.
I wrote a simple example, you can refer to it.
Model
Index
test1
Here is the result.
Best Regards,
YihuiSun
None
0 Points
3 Posts
Re: How to Dynamically Append to a Base URL using HttpClient
Aug 24, 2020 02:18 PM|ronniej962|LINK
Thank you YihuiSun!!!
I will give this a try and follow up!!!