如何在本地主机端口443上运行ASP.NET Core MVC网页

问题描述 投票:0回答:1

我正在使用 Visual Studio 2022,并在 .NET 6.0 中创建了 ASP.NET Core MVC Web 应用程序。我可以更改设置以使用非 https URL,它可以工作。该页面最终需要在我们的 QA 和生产环境中在 https 上运行。我不使用 IIS。

如果我将

launchsettings.json
配置为使用 https、端口 443,则会收到错误。当我尝试使用 URL
https://localhost:44337
:

在本地运行页面时

该网站的连接不安全
本地主机发送了无效的响应。
尝试运行 Windows 网络诊断。
ERR_SSL_PROTOCOL_ERROR

这是我的

launchSettings.json
文件:

{
  "profiles": {
    "MicroGraphicsMVC": {
      "applicationUrl": "https://localhost:44337",
      "commandName": "Project",
      "dotnetRunMessages": true,
      "environmentVariables": {
        "ASPNETCORE_ENVIRONMENT": "Development"
      },
      "launchBrowser": true,
      "sslPort": 44337
    }
  }
}

在我的

program.cs
文件中,我有:

var builder = WebApplication.CreateBuilder(args);

////configure listening port
builder.WebHost.ConfigureKestrel(options => options.Listen(IPAddress.Any, 44337));

// Add services to the container.
builder.Services.AddControllersWithViews();

//add Dependency Injection for our objects
builder.Services.AddScoped<IMicroGraphics, MicroGraphicsRepo>();

var app = builder.Build();

// Configure the HTTP request pipeline.
if (!app.Environment.IsDevelopment())
{
    app.UseExceptionHandler("/Home/Error");
    app.UseHsts();
}

//app.UseHttpsRedirection();
//app.UseStaticFiles();

app.UseRouting();

//app.UseAuthorization();

app.MapControllerRoute(
    name: "default",
    pattern: "{controller=Home}/{action=Index}/{id?}");

app.Run();

如何让我的页面在 https 上本地运行?

c# asp.net-core asp.net-core-mvc visual-studio-2022
1个回答
0
投票

如果您使用的是 Windows,请在项目的根路径中执行以下命令以信任开发证书:

dotnet dev-certs https --trust

此命令可确保自签名开发证书受到操作系统的信任,从而促进应用程序的安全通信。

© www.soinside.com 2019 - 2024. All rights reserved.