我多次尝试从 Visual Studio 2022 和 2019 创建新的剃刀项目
样板模板如下,但是当我单击调试按钮时,控制台输出其监听正常,如下所示
Microsoft.Hosting.Lifetime: Information: Now listening on: https://localhost:7277
Microsoft.Hosting.Lifetime: Information: Now listening on: http://localhost:5227
Microsoft.Hosting.Lifetime: Information: Application started. Press Ctrl+C to shut down.
Microsoft.Hosting.Lifetime: Information: Hosting environment: Development
但是,当我导航到这些网址中的任何一个时,我会收到 404。
但是,如果我通过控制台执行 dotnet run 或 dotnet watch,则运行正常,不会出现 404 错误。
这是我的 LunchSettings.json 文件(未触及样板文件)
{
"iisSettings": {
"windowsAuthentication": false,
"anonymousAuthentication": true,
"iisExpress": {
"applicationUrl": "http://localhost:25708",
"sslPort": 44374
}
},
"profiles": {
"http": {
"commandName": "Project",
"dotnetRunMessages": true,
"launchBrowser": true,
"applicationUrl": "http://localhost:5227",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
},
"https": {
"commandName": "Project",
"dotnetRunMessages": true,
"launchBrowser": true,
"applicationUrl": "https://localhost:7277;http://localhost:5227",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
},
"IIS Express": {
"commandName": "IISExpress",
"launchBrowser": true,
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
}
}
}
和Program.cs
var builder = WebApplication.CreateBuilder(args);
// Add services to the container.
builder.Services.AddRazorPages();
var app = builder.Build();
// Configure the HTTP request pipeline.
if (!app.Environment.IsDevelopment())
{
app.UseExceptionHandler("/Error");
// The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
app.UseHsts();
}
app.UseHttpsRedirection();
app.UseStaticFiles();
app.UseRouting();
app.UseAuthorization();
app.MapRazorPages();
app.Run();
和文件夹结构(与 vs 样板相比均未更改)
这是调试箱
我尝试重置 vs 设置并启动多个项目
经过大量研究,我设法解决了这个问题,但只能完全卸载 Visual Studio 并重新安装。