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

Windows10环境下运行.NET Core Web API遇程序集加载失败问题

解决.NET Core Web API运行时BadImageFormatException且无法修改NuGet版本的问题

错误信息

fail: Microsoft.AspNetCore.Server.Kestrel[13]
          Connection id "0HMLLR56JLU26", Request id "0HMLLR56JLU26:0000000B": An unhandled exception was thrown by the application.
    System.BadImageFormatException: Could not load file or assembly 'Microsoft.AspNetCore.Http.Extensions, Version=3.1.0.0, Culture=neutral, PublicKeyToken=adb9793829ddae60'. Format of the executable (.exe) or library (.dll) is invalid.
    File name: 'Microsoft.AspNetCore.Http.Extensions, Version=3.1.0.0, Culture=neutral, PublicKeyToken=adb9793829ddae60'
       at Microsoft.AspNetCore.StaticFiles.StaticFileContext.get_RequestHeaders
       at Microsoft.AspNetCore.StaticFiles.StaticFileContext.ComputeIfMatch()
       at Microsoft.AspNetCore.StaticFiles.StaticFileContext.ComprehendRequestHeaders()
       at Microsoft.AspNetCore.StaticFiles.StaticFileContext.ServeStaticFile(HttpContext context, RequestDelegate next)

解决步骤

  • 匹配项目目标平台与系统位数:右键项目 → 属性 → 生成选项卡,设置目标平台为x64(或与当前Windows系统一致的位数),同时取消勾选首选32位选项,保存后重新编译。
  • 清理并重建项目:在项目根目录打开命令提示符,依次执行以下命令:
    dotnet clean
    dotnet restore
    dotnet build
    
  • 验证.NET Core SDK版本:执行dotnet --list-sdks查看已安装的SDK,确保存在3.1.x版本。若缺失,安装对应版本的SDK(项目依赖3.1版本,必须匹配)。
  • 重置NuGet缓存:删除本地NuGet缓存目录C:\Users\<你的用户名>\.nuget\packages,之后重新执行dotnet restore还原依赖。
  • 检查runtime配置:打开项目输出目录下的<项目名>.runtimeconfig.json文件,确认runtimeOptions里的version字段与项目依赖的.NET Core 3.1版本一致,若不一致手动修正后重新运行。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.15 06:25:19