[英]Encoding apostrophe in GET API request via "HttpUtility.UrlEncode" and "Uri.EscapeDataString" gives error
[英]Uri.EscapeDataString or HttpUtility.UrlEncode does not work for my case
我在一個項目中有一個API,我將其編碼如下:
[Route("api/SearchCustomer/{customer}")]
[HttpGet]
public List<Customer> SearchCustomer(string customer)
{
return customerRepo.GetSearchJoined(customer);
}
首先,當我從前端調用此API時遇到問題,如果客戶包含點或空格(例如: https : //www.somesite.com/walmart Inc.),則會收到404錯誤(無法找到了此API)。 我找到一種解決此問題的簡便方法。 只需添加“ /”即可解決此問題。( https://www.somesite.com/walmart Inc./)
現在,我需要在后端的另一個項目中調用此API。 所以我做了這樣的事情:
var urlParm = "https://www.somesite.com/api/SearchCustomer/" + name + "/";
response = client.GetAsync(urlParm).Result;
var dataObjects = response.IsSuccessStatusCode ? response.Content.ReadAsAsync<IList<Customer>>().Result : null;
return dataObjects;
不幸的是,在后面添加“ /”不起作用。 我仍然收到404錯誤。 然后,我嘗試使用Uri.EscapeDataString或HttpUtility.UrlEncode編碼“名稱”。( C#是否等效於JavaScript的encodeURIComponent()? )
name = Uri.EscapeDataString(name)
or name = HttpUtility.UrlEncode(name)
or name = HttpUtility.UrlPathEncode(name)
var urlParm = "https://www.somesite.com/api/SearchCustomer/" + name + "/";
or var urlParm = = "https://www.somesite.com/api/SearchCustomer/" + name
response = client.GetAsync(urlParm).Result;
var dataObjects = response.IsSuccessStatusCode ? response.Content.ReadAsAsync<IList<Customer>>().Result : null;
return dataObjects;
我已經嘗試了以上代碼的所有不同匹配。 他們都沒有工作。 我仍然收到404錯誤。 有人知道我在這里做錯了嗎?
很抱歉輸入錯誤,我刪除了一些敏感信息,所以我誤刪除了“ api”。 路線不是問題。 我已經測試了從后端的api調用是否可以在if名稱僅包含字母或數字的情況下進行,但在name包含點時失敗。
問題與customer
參數是否編碼無關。 您應該指定路由並正確應用請求。 首先確定路線;
[Route("api/SearchCustomer/{customer}")]
然后應用請求。
https://www.somesite.com/api/SearchCustomer/samplecustomer
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.