React Native:为什么将查询参数附加到我的 .Net API 调用会自动返回 404 错误? [英] React Native: Why does appending query parameters to my .Net API calls automatically return a 404 error?

查看:69
本文介绍了React Native:为什么将查询参数附加到我的 .Net API 调用会自动返回 404 错误?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的 React Native 应用程序中,我试图访问用 .Net 编写的 API 中的端点.我的本地机器上有 API 代码,所以我试图通过调用 fetch(127.0.0.1:5000/api/token) 来访问它.这将返回 404 错误.如果我改为点击 fetch(127.0.0.1:5000/),它会成功返回.本例中命中的 API 代码是 HomeController.cs,其中包含:

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";
        }
    }
}

当我调用 fetch(127.0.0.1:5000/) 时,它返回Ok"并命中我在 HomeController.cs 中设置的断点.当我调用 fetch(127.0.0.1:5000/api/token) 时应该命中的另一个文件 ValuesController.cs 包含此代码(加上后面的更多内容):

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]")]
    ...
    ...

当我调用 fetch(127.0.0.1:5000/api/token) 时,没有命中 ValuesController.cs 中的断点,并返回 404 错误.

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.

我试图做的来解决这个问题,因为 HomeController.cs 正在工作,我用 [Route> 替换了 [Route("/")]("/api")],并调用fetch(127.0.0.1:5000/api).这也返回了 404 错误.

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.

我想知道的:

为什么使用 fetch(127.0.0.1:5000/) 访问包含 [Route("/")] 的 API 代码有效,但将其修改为使用 fetch(127.0.0.1:5000/api) 命中 [Route("/api")] 会抛出 404 错误?

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?

推荐答案

问题在于路由.

[Route("/api/[controller]")]

此 RouteAttribute 定义会将端点路由到控制器的名称.

This RouteAttribute definition will route the endpoint to the name of the controller.

应该点击的另一个文件 ValuesController.cs...

The other file ValuesController.cs that should be hit...

好像您的控制器名称是 ValuesController,它会在 /api/values 上创建端点,而不是 /api/token.

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:

  1. 将控制器的名称从 ValuesController 更改为 TokenController.
  2. 将 RouteAttribute 从 [Route("/api/[controller]")] 更改为 [Route("/api/token")].

这篇关于React Native:为什么将查询参数附加到我的 .Net API 调用会自动返回 404 错误?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆