You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在本地HTTPS环境运行ASP.NET Core MVC网页并修复CSS失效

本地运行.NET 6.0 ASP.NET Core MVC HTTPS站点问题

我使用Visual Studio 2022创建了.NET 6.0的ASP.NET Core MVC Web应用,将配置改为非HTTPS URL时可正常运行,但该应用最终需在QA及生产环境以HTTPS运行,且我不使用IIS。若将launchsettings.json配置为使用HTTPS(端口44337),访问https://localhost:44337时会出现如下错误:

此站点的连接不安全
localhost发送了无效响应。
尝试运行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运行?


补充:按照王先生的建议处理后,错误不再出现,但网页的CSS/样式失效,效果如下:
CSS失效的网页截图


内容的提问来源于stack exchange,提问作者WorkJ

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.02 12:04:57