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

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.21 05:37:05