In my React Native app, I'm trying to hit an endpoint in an API written in .Net. I've got the API code on my local machine, so I'm trying to hit it with a call to fetch(127.0.0.1:5000/api/token)
. This returns a 404 error. If I instead hit fetch(127.0.0.1:5000/)
, it returns successfully. The API code hit in this case is HomeController.cs
, which contains this:
namespace InTowAPI2.Controllers
{
[AllowAnonymous]
[Route("/a")]
public class HomeController : Controller
{
// GET api/values
[HttpGet]
public string Get()
{
return "Ok";
}
}
}
When I make the call to fetch(127.0.0.1:5000/)
, it returns "Ok" and hits the breakpoint I set in HomeController.cs
. The other file ValuesController.cs
that should be hit when I call fetch(127.0.0.1:5000/api/token)
contains this code (plus more after it):
namespace InTowAPI2.Controllers
{
[AllowAnonymous]
[Route("/api/[controller]")]
...
...
When I call fetch(127.0.0.1:5000/api/token)
, the breakpoint in ValuesController.cs
is not hit, and it returns a 404 error.
What I tried to do to troubleshoot this was, since HomeController.cs
was working, I replaced [Route("/")]
with [Route("/api")]
, and made a call to fetch(127.0.0.1:5000/api)
. This also returned a 404 error.
What I Want To Know:
Why is it that hitting the API code containing [Route("/")]
with fetch(127.0.0.1:5000/)
works, but modifying it to hit [Route("/api")]
with fetch(127.0.0.1:5000/api)
throws a 404 error?
The issue is the routing.
[Route("/api/[controller]")]
This RouteAttribute definition will route the endpoint to the name of the controller.
The other file ValuesController.cs that should be hit...
Seems like you controller name is ValuesController, which would create an endpoint on /api/values, not /api/token.
To fix this, do one of the following things: