ASP.NET Core 7 Razor Pages应用执行dotnet <appname>.dll返回404的排查与解决
ASP.NET Core 7 Razor Pages构建运行404问题解决
问题现象
- 命令行执行
dotnet build <app>.csproj --configuration Release构建应用后,运行dotnet <app>.dll,所有请求均返回HTTP 404错误 - 但通过Visual Studio直接启动应用,或VS构建后再执行相同运行命令,功能完全正常
原项目launchSettings.json配置
"iisSettings": { "windowsAuthentication": false, "anonymousAuthentication": true, "iisExpress": { "applicationUrl": "http://localhost:18790", "sslPort": 44367 } }, "profiles": { "IIS Express": { "commandName": "IISExpress", "launchBrowser": true, "environmentVariables": { "ASPNETCORE_ENVIRONMENT": "Development" }, "ancmHostingModel": "InProcess" }, "<app>": { "commandName": "Project", "launchBrowser": true, "environmentVariables": { "ASPNETCORE_ENVIRONMENT": "Development" }, "dotnetRunMessages": "true", "applicationUrl": "https://localhost:5001;http://localhost:5000" } } }
解决方法
在项目根目录添加global.json文件,锁定SDK版本为项目依赖的7.0.101:
{ "sdk": { "version": "7.0.101" }, "tools": { "dotnet": "7.0.101" } }
原因分析
核心问题是本地环境安装的.NET SDK版本与项目依赖的7.0.101不一致,命令行构建时默认使用了不匹配的SDK版本,导致Razor Pages的视图编译、路由注册等环节出现兼容性问题,最终引发404错误。通过global.json明确指定SDK版本后,构建流程会使用匹配的SDK编译,确保运行时与项目依赖完全一致。
内容的提问来源于stack exchange,提问作者Andrii Voitovych
相关产品推荐
相关产品推荐

