简体   繁体   中英

"404Not Found" error when testing a Rest API by Postman

I have a very simple Rest API written with c#:

namespace Test.Controllers
{
    [Route("api/[action]")]
    [ApiController]
    public class NewController : ControllerBase
    {
        [HttpGet]
        public bool stringCheck(string value)
        {
            if (value.Contains("love"))
                return true;
            else
                return false;
        }
    }
}

And this is the command I use in Postman:

http://localhost:5000/api/stringcheck?str=my love

I don't get any compiler errors, but no matter what I pass as the value in Postman, I always get the 404. What should I do?

The problem is from your [Route] values, you can't use [Route("api/[action]")] above of a controller since it may cause ambiguity, you need a little change to get the result, the change is specifying the controller too, like below:

[ApiController]
[Route("api/[controller]/[action]")]
public class NewController : ControllerBase

And pay attention that the queryString parameter should exactly be as same as the method parameter so in postman you should call the below URL to get the result:

http://localhost:5000/api/new/stringcheck?value=hamed

fell free to ask questions. good luck.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM