【问题标题】:How to run asp.net core 2.1 release app at a specific port?如何在特定端口运行 asp.net core 2.1 发布应用程序?
【发布时间】:2019-04-08 13:02:31
【问题描述】:

环境:Ubuntu 18.04下的ASP.NET Core 2.1

默认情况下,新创建的 asp.net 核心应用在http://localhost:5000 运行并自动重定向到https://localhost:5001

由于我打算在 nginx 下运行我的应用程序(并使用 nginx 的 SSL 功能),我想在没有 https 的情况下在特定端口(例如 6000)上运行我发布的应用程序。

帖子ASP.NET Core 2.1 + Kestrel (How to disable HTTPS) 解释说,必须简单地使用“--urls”参数来实现这一点。

这是我尝试过的:

$ dotnet publish --configuration Release
$ dotnet bin/Release/netcoreapp2.1/MyApp.dll

该应用现在开始在http://localhost:5000https://localhost:5001 上收听。当我浏览到http://localhost:5000 时,它会自动重定向到https://localhost:5001。到目前为止一切顺利。

现在,我尝试在 6000 端口运行:

$ dotnet bin/Release/netcoreapp2.1/MyApp.dll
     --urls="http://localhost:6000"

从终端窗口中的消息来看,该应用似乎开始在http://localhost:6000 监听。但是,浏览到此 url 会导致“无法访问此站点”错误。

我什至尝试过:

$ dotnet bin/Release/netcoreapp2.1/MyApp.dll
     --urls="http://localhost:6000,https://localhost:6001"

在这种情况下,我收到“路径基只能使用 IApplicationBuilder.UsePathBase() 配置”的错误。

谁能告诉我如何在指定端口简单地运行应用程序?我希望我不需要硬编码端口号。

【问题讨论】:

    标签: asp.net-core


    【解决方案1】:

    "路径基只能使用 IApplicationBuilder.UsePathBase() 来配置。"

    这个错误是因为你在url之间使用了,。您需要使用; 来拆分网址,如下所示:

    dotnet bin/release/netcoreapp2.1/publish/webapplication3.dll --urls="http://localhost:8000;https://localhost:8001"
    

    我想在没有 https 的情况下在特定端口(例如 6000)上运行我发布的应用程序。

    对于没有https的运行,您需要删除Startup.cs中的app.UseHttpsRedirection();

    补充说明

    如果你无法访问6000,我建议你用8000进行测试,port 6000被大多数浏览器禁用。

    【讨论】:

    • 感谢您的帮助。我应该测试一个不同于 6000 的端口:-(
    【解决方案2】:

    根据this piece of documentation,它应该类似于dotnet run --urls "http://*:8080"。因此,两者之间没有等号。但是根据Kestrel 文档,有多种方法可以定义不同的端口,例如ASPNETCORE_URLS 环境变量。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-04-29
      • 2019-02-11
      • 2019-05-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多