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

.NET MAUI Blazor Hybrid iOS构建报错:appsettings.json路径超出应用包范围

解决.NET MAUI Blazor Hybrid iOS项目appsettings.json路径错误问题
  • 调整文件的生成操作配置
    确保appsettings.json的生成操作设置为Content或Embedded resource,同时设置Copy to Output Directory为Copy if newer。可以直接编辑项目的.csproj文件添加以下配置:

    <ItemGroup>
      <Content Include="appsettings.json">
        <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
      </Content>
    </ItemGroup>
    
  • 修正代码中的路径引用
    避免使用多层嵌套的相对路径读取文件,改用基于项目输出目录的相对路径。示例代码如下:

    var configBuilder = new ConfigurationBuilder()
        .SetBasePath(Directory.GetCurrentDirectory())
        .AddJsonFile("appsettings.json", optional: false, reloadOnChange: true);
    var configuration = configBuilder.Build();
    
  • 清理缓存并重新构建
    执行以下命令清理项目缓存后重新构建iOS目标:

    dotnet clean
    dotnet build -f net8.0-ios
    
  • 针对iOS平台单独配置资源
    在.csproj中添加iOS平台专属的资源配置,确保文件被正确打包进iOS应用包:

    <ItemGroup Condition="'$(TargetFramework)' == 'net8.0-ios'">
      <BundleResource Include="appsettings.json">
        <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
      </BundleResource>
    </ItemGroup>
    

内容的提问来源于stack exchange,提问作者Vinh Ngo

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.20 17:17:33