8wDlpd.png
8wDFp9.png
8wDEOx.png
8wDMfH.png
8wDKte.png

WebApplicationBuilder.Services.AddControllers 导致 FileNotFoundException

Gerasimos Ragavanis 2月前

24 0

使用 .NET 8 - 我的 Web 应用程序在调试时运行良好,但当我将其发布到文件夹时,它会引发 FileNotFound 异常。未处理的异常。System.IO.FileNotFoundException:无法加载文件或

使用.NET 8-我的 Web 应用程序在调试时运行良好,但是当我将其发布到文件夹时,它会引发异常 FileNotFound

未处理的异常。System.IO.FileNotFoundException:无法加载文件或程序集“Swashbuckle.AspNetCore.SwaggerGen,Culture=neutral,PublicKeyToken=null”。系统找不到指定的文件。文件名:“Swashbuckle.AspNetCore.SwaggerGen,Culture=neutral,PublicKeyToken=null”

我已确保已 Swashbuckle.AspNetCore.SwaggerGen 安装 NuGet 包。

语境:

WebApplicationBuilder builder = WebApplication.CreateBuilder(args);

builder.Services.AddControllers(); // this line here

builder.Services.AddDistributedMemoryCache();
builder.Services.AddSession(options =>
{
    options.IdleTimeout = TimeSpan.FromSeconds(60);
    options.Cookie.HttpOnly = false;
    options.Cookie.IsEssential = true;
});

var app = builder.Build();

if (!app.Environment.IsDevelopment())
{
    app.UseHsts();
}

app.UseHttpsRedirection();
app.UseStaticFiles();
app.UseRouting();
app.UseSession();

app.MapControllerRoute("default", "{controller}/{action=Index}/{id?}");
app.MapFallbackToFile("index.html");

return app;

编辑:Jason Pan 要求提供 csproj 文件。这里是


  <PropertyGroup>
    <TargetFramework>net8.0</TargetFramework>
    <Nullable>enable</Nullable>
    <ImplicitUsings>enable</ImplicitUsings>
    <SpaRoot>..\prodschedule.client</SpaRoot>
    <SpaProxyLaunchCommand>npm start</SpaProxyLaunchCommand>
    <SpaProxyServerUrl>https://localhost:4200</SpaProxyServerUrl>
  </PropertyGroup>

  <ItemGroup>
    <PackageReference Include="Microsoft.AspNetCore.SpaProxy">
      <Version>8.*-*</Version>
    </PackageReference>
    <PackageReference Include="Microsoft.Extensions.ApiDescription.Server" Version="8.0.8">
      <PrivateAssets>all</PrivateAssets>
      <IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
    </PackageReference>
    <PackageReference Include="Microsoft.OpenApi" Version="1.6.18" />
    <PackageReference Include="Swashbuckle.AspNetCore" Version="6.7.3" />
    <PackageReference Include="Swashbuckle.AspNetCore.Swagger" Version="6.7.3" />
    <PackageReference Include="Swashbuckle.AspNetCore.SwaggerGen" Version="6.7.3" />
    <PackageReference Include="Swashbuckle.AspNetCore.SwaggerUI" Version="6.7.3" />
  </ItemGroup>

  <ItemGroup>
    <Content Include="Sql\**">
      <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
    </Content>
  </ItemGroup>

  <ItemGroup>
    <ProjectReference Include="..\prodschedule.client\prodschedule.client.esproj">
      <ReferenceOutputAssembly>false</ReferenceOutputAssembly>
    </ProjectReference>
    <ProjectReference Include="W:\Improvement\Client Subscriptions\SubHashes\SubHashes.csproj" />
    <ProjectReference Include="W:\Improvement\Libraries\CSharp\ApiLibrary\Holidays\Holidays.csproj" />
    <ProjectReference Include="W:\Improvement\Libraries\CSharp\ApiLibrary\Http\Http.csproj" />
    <ProjectReference Include="W:\Improvement\Libraries\CSharp\ApiLibrary\Ostendo\Ostendo.csproj" />
    <ProjectReference Include="W:\Improvement\Libraries\CSharp\CSLogger\CSLogger.csproj" />
    <ProjectReference Include="W:\Improvement\Libraries\CSharp\Utility\Utility.csproj" />
  </ItemGroup>

</Project>
帖子版权声明 1、本帖标题:WebApplicationBuilder.Services.AddControllers 导致 FileNotFoundException
    本站网址:http://xjnalaquan.com/
2、本网站的资源部分来源于网络,如有侵权,请联系站长进行删除处理。
3、会员发帖仅代表会员个人观点,并不代表本站赞同其观点和对其真实性负责。
4、本站一律禁止以任何方式发布或转载任何违法的相关信息,访客发现请向站长举报
5、站长邮箱:yeweds@126.com 除非注明,本帖由Gerasimos Ragavanis在本站《asp.net-core》版块原创发布, 转载请注明出处!
最新回复 (0)
  • 请尝试手动将 Swashbuckle.AspNetCore.SwaggerGen.dll 文件复制到发布文件夹,查看是否存在其他问题。如果一切正常,您可以考虑在发布项目时包含 Swashbuckle.AspNetCore.SwaggerGen.dll 文件。

  • 我以为这个错误只是因为中间件而引发的。添加这个 AddControllerWithViews(); 中间件而不是添加这个 AddController(); 就可以解决您的问题。

返回
作者最近主题: