[英]Is there a way to pass properly a string containing a “?” to .net core web api?
我有以下操作,可以從我的API中獲取問題模型,該問題模型來自自動完成輸入,給出了問題標題。 此操作適用於不帶問號的標題(例如,您幾歲)。 但是,如果我給出的標題包含問號(例如,您幾歲?),則不會返回該模型,因為在此過程中將刪除問號。
我嘗試了HttpUtility.UrlDecode()
方法,但是沒有運氣。
您可以在下面找到我的要求
[HttpGet]
public async Task<IActionResult> GetQuestionAsync(string question) {
Questions q = new Questions();
HttpClient client = _api.Initial();
HttpResponseMessage res = await client.GetAsync("api/Search/" + question);
if (res.IsSuccessStatusCode) {
var result = res.Content.ReadAsStringAsync().Result;
q = JsonConvert.DeserializeObject<Questions>(result);
}
return View(q);
}
[Produces("application/json")]
[HttpGet]
[Route("{keyword}")]
public async Task<IActionResult> GetByString([FromRoute(Name = "keyword")] string keyword) {
if (!ModelState.IsValid) {
return BadRequest(ModelState);
}
var question = await _context.Questions
.SingleOrDefaultAsync(m => m.Question == HttpUtility
.UrlDecode(keyword.ToString()));
if (question == null) {
return NotFound();
}
return Ok(question);
}
我希望能夠收到包括以下內容的問題?
從我的API。 有沒有辦法做到這一點?
請注意,在Swagger中,API Get請求工作正常!
您需要使用HttpUtility.UrlEncode
而非Decode
。 您要更改?
編碼后的字符,然后再通過URL發送。 HttpUtility.UrlDecode
則相反。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.