Visual Studio调试新建Razor项目时出现404错误
问题:Visual Studio调试Razor项目出现404,dotnet命令可正常运行
多次在Visual Studio 2022和2019中创建默认模板的Razor项目,点击调试按钮后,控制台显示服务正常监听:
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
但访问上述任一URL都会出现404错误。不过通过控制台执行dotnet run或dotnet watch命令时,项目可正常运行无404问题。
相关文件内容
launchSettings.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();
项目结构截图
- 文件夹结构:

- 调试bin目录:

已尝试重置VS设置并创建多个项目,但问题依旧。
内容的提问来源于stack exchange,提问作者Lawzzz
相关产品推荐
相关产品推荐

