简体   繁体   English

有没有办法将包含“?”的字符串正确传递给.net核心Web API?

[英]Is there a way to pass properly a string containing a “?” to .net core web api?

I have the following action that gets a Question model from my API given the Question title from an autocomplete input. 我有以下操作,可以从我的API中获取问题模型,该问题模型来自自动完成输入,给出了问题标题。 The action is working fine with titles that do not contain a question mark (Ex. How old are you). 此操作适用于不带问号的标题(例如,您几岁)。 But if I give a title containing a question mark (Ex. How old are you?) the model is not returned as the question mark is being removed in the process. 但是,如果我给出的标题包含问号(例如,您几岁?),则不会返回该模型,因为在此过程中将删除问号。

I tried HttpUtility.UrlDecode() method but with no luck . 我尝试了HttpUtility.UrlDecode()方法,但是没有运气。

Below you can find my requests 您可以在下面找到我的要求

[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);
}

I expect to be able to get questions including ? 我希望能够收到包括以下内容的问题? from my API. 从我的API。 Is there a way to achieve this? 有没有办法做到这一点?

Note that in Swagger, the API Get request is working fine! 请注意,在Swagger中,API Get请求工作正常!

You need to use HttpUtility.UrlEncode - not Decode . 您需要使用HttpUtility.UrlEncode而非Decode You want to change the ? 您要更改? into an encoded character before sending it in the URL. 编码后的字符,然后再通过URL发送。 HttpUtility.UrlDecode does the opposite. HttpUtility.UrlDecode则相反。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 将对象引用传递到 .NET Core Web API 的正确方法 - Correct way to pass object reference into .NET Core Web API 如何在 .net core 2.2 web API 中传递带有“禁止”响应的字符串 - How can pass string with the “forbidden” response in .net core 2.2 web API 如何将值作为字符串数组传递给 ASP.NET 核心 Web ZDB974238714CA8DE634A7CE1D08 中的 json 键 - How to pass value as string array to json key in ASP.NET Core Web API 如何正确创建 ASP.NET 内核 Web API - How to create ASP.NET Core Web API properly .Net Core Web Api-无法正确授权,获得401 - .Net Core Web Api - unable to Authorize properly, getting 401 如何在asp net core web API上正确地将列表转换为IQueryable - How to properly convert a list to IQueryable on a asp net core web API 如何在ASP.NET Core Web API中传递int数组 - How to pass an array of int in ASP.NET Core Web API 无法将文件传递到 ASP.NET MVC 核心中的 web api - Unable to pass file to web api in ASP.NET MVC Core .net 核心 web api - Z594C103F2C6E04C3D8AB059F031E0 可以传递参数到中间件吗? - .net core web api - Can controller pass parameters to middleware? NET Core Web API中如何使用HTTP get方法传递参数? - how to pass a argument with HTTP get method in .net core web api?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM