我有一個 REST 端點,我需要在引號內傳遞如下輸入引數
/api/rooms?filter=name='A1'
如果我不使用引號傳遞引數,它將出錯。因此,在我的代碼中,如何使用單引號呼叫帶有輸入引數的端點并獲得回應。以下是我嘗試過但不確定它是否正確的方法
public async Task<string> GetRoomID(string roomName)
{
using (var client = new HttpClient())
{
client.BaseAddress = new Uri(_iconfiguration.GetSection("RoomMovement").GetSection("BaseAddress").Value);
client.DefaultRequestHeaders.Accept.Clear();
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
HttpResponseMessage response = await client.GetAsync("api/rooms?filter=name=" roomName).ConfigureAwait(false);
if (response.IsSuccessStatusCode)
{
string result = response.Content.ReadAsStringAsync().Result;
uj5u.com熱心網友回復:
你可以試試這個:
HttpResponseMessage response = await client.GetAsync($"api/rooms?filter=name='{roomName}'").ConfigureAwait(false);
uj5u.com熱心網友回復:
你試過這個嗎?您可以將 's 單獨添加到您的字串中!
HttpResponseMessage response = await client.GetAsync("api/rooms?filter=name=" "'" roomName "'").ConfigureAwait(false);
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/472706.html
上一篇:登出清除會話資料
下一篇:C#中的方法多載
