【问题标题】:.NET 5 webapi example returning 404, running in Linux.NET 5 webapi 示例返回 404,在 Linux 中运行
【发布时间】:2021-06-01 16:24:17
【问题描述】:

我正在尝试在 Linux 上使用 .NET 5.0,使用 VS Code。

我创建了一个空目录,运行“code”。在 VS Code 中打开它,然后创建示例项目:

dotnet new webapi -n 实验

当“'实验'中缺少构建和调试所需的资产。添加它们?”出现对话框,我点击“是”。

我运行了构建任务,然后从“运行”菜单中选择了“开始调试”。

该项目似乎正在运行,它打开了一个 Chrome 浏览器选项卡到 https://localhost:5001/,该选项卡显示 HTTP ERROR 404。

lauchSettings.json 中的“Expementing”配置文件将 applicationUrl 设置为“https://localhost:5001;http://localhost:5000”,调试控制台包括:

现在监听:https://localhost:5001 现在收听:http://localhost:5000

所以端口似乎是正确的。

如果我尝试在 http://localhost:5000 上打开浏览器选项卡,它会重定向到 https://localhost:5001,然后显示 404。

我错过了什么?


jmoerdyk 的评论是正确的 - 如果我尝试 https://localhost:5001/swagger,我会得到 swagger 页面。

在 .NET Framework 中,Web API 在域根目录创建登录页面。 .NET Core 没有。

那么,问题是为什么浏览器选项卡会在 https://localhost:5001 而不是 https://localhost:5001/swagger 打开?

我在 launchSettings.json 中注意到了这一点:

"profiles": {
    "IIS Express": {
        ...
    },
    "Experimenting": {
        "commandName": "Project",
        "dotnetRunMessages": "true",
        "launchBrowser": true,
        "launchUrl": "swagger",
        "applicationUrl": "https://localhost:5001;http://localhost:5000",
        "environmentVariables": {
            "ASPNETCORE_ENVIRONMENT": "Development"
        }
    }
}

我认为应该打开指向招摇页面的浏览器。浏览一下,我看到了 VS Code 不使用 launchSettings.json 的声明,但很明显,因为如果我更改 applicationUrl 中的端口,我会在新端口上运行招摇页面。

但launchUrl似乎被忽略了。

浏览网页给了我同样的错误和过时的答案,这在 .NET Core 问题中很常见,但我最终尝试在 launch.json 中设置 uriFormat:

{
    "version": "0.2.0",
    "configurations": [
        {
            "name": ".NET Core Launch (web)",
            ...
            // Enable launching a web browser when ASP.NET Core starts. For more information: https://aka.ms/VSCode-CS-LaunchJson-WebBrowser
            "serverReadyAction": {
                "action": "openExternally",
                "pattern": "\\bNow listening on:\\s+(https?://\\S+)",
                "uriFormat": "%s/swagger"
            },
            ...
        },
    ]
}

评论中的链接解释了这应该如何工作 - 有点。

https://aka.ms/VSCode-CS-LaunchJson-WebBrowser

【问题讨论】:

  • 正如预期的那样。在模板创建的代码中,根路径或默认路由上没有端点,因此404 Not Found。当您访问https://localhost:5001/swagger/https://localhost:5001/WeatherForecast 时会发生什么?

标签: linux visual-studio-code


【解决方案1】:

404 代码的原因是你已经知道没有对 root 的默认 GET 请求。

根据这条记录,它告诉launchSettings.json 仅用于Visual Studio 调试。我在 Windows 上使用 VS 和在 Linux 上使用 dotnet CLI 尝试过项目,我可以确认这种体验。

Is launchSettings.json used when running ASP.NET 5 apps from the command line on Mac?

您可以使用重定向作为路由的替代方式,以将 Swagger 作为默认设置,而不是 launchSettings.json 文件。您可以在 Controllers 文件夹中创建此类(例如 RouteRedirectController.cs),并且在根 GET 调用的情况下它将重定向到 swagger:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging;

namespace TestAPI.Controllers
{
    [ApiController]
    [Route("")]
    public class RouteRedirectController : ControllerBase
    {
        [HttpGet]
        public IActionResult Get()
        {
            return Redirect("~/swagger");
        }
    }
}

更新:说实话,这有点奇怪,因为如果我在 launchSettings.json 文件中更改“applicationUrl”(不适用于适用于 Windows 的 IIS Express 配置文件),那么更改会在之后反映dotnet run 命令被执行。忽略launchUrl 可能是dotnet run 期间的错误?但如果它仍然存在,那么它可以通过这个重定向端点来解决。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-02-24
    相关资源
    最近更新 更多