首页 > 解决方案 > .NET5 Web API 返回 404 Not Found

问题描述

我正在本地计算机 (Win10) 上设置我的第一个 .NET5 Web API。为了测试,我制作了这个简单的控制器:

namespace API.Controllers
{
    public class TestController : Controller
    {
        [HttpGet]
        [Route("/test/hallo")]
        public string TestMethod()
        {
            return "Hallo";
        }
    }
}

从 VS2019 运行/调试代码时,会打开此 URL:https://localhost:5001/swagger/index.html

当我在 Swagger 页面中执行“test/hallo”时,它会按预期返回“Hallo”。

请求 URL 为:https://localhost:5001/test/hallo

到目前为止,一切都很好!

当我尝试从 Google Advanced REST Client 运行 API 时,问题就来了。我在请求 URL 中选择 Method = "Get" 和 "https://api.mydomain/test/hallo"。它返回 404 Not Found。

本地域“api.mydomain”在我的计算机上使用自签名证书。它似乎工作正常,我已经通过在项目根目录中添加“test.html”来测试它。在 Google Chrome 中打开https://api.mydomain/test.html时,页面显示正确。

最大的问题是:尝试使用 Google 高级 REST 客户端访问具有本地域 (api.mydomain) 的 API 时,为什么会得到“404 Not Found”?

标签: .netasp.net-web-apiasp.net-web-api-routing.net-5

解决方案


推荐阅读