Asp.net-Core

無法載入嵌入文件清單“Microsoft.Extensions.FileProviders.Embedded.Manifest.xml”ASP.NET Core 3.0

  • September 15, 2021

我有一個 asp.net core 3.0 網站,我正在嘗試使用 FileProvider。我根據範例創建了以下內容,但我不斷收到錯誤消息

InvalidOperationException:無法為程序集“Test”載入嵌入文件清單“Microsoft.Extensions.FileProviders.Embedded.Manifest.xml”。

下面是我的啟動課

using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Threading.Tasks;
using IntranetPages.Shared;
using Microsoft.AspNetCore.Authentication;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.HttpsPolicy;
using Microsoft.AspNetCore.Server.IISIntegration;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.FileProviders;
using Microsoft.Extensions.Hosting;

namespace Test
{
   public class Startup
   {
       private readonly IWebHostEnvironment _env;

       public Startup(IWebHostEnvironment env, IConfiguration configuration)
       {
           Configuration = configuration;
           _env = env;
       }

       public IConfiguration Configuration { get; }

       // This method gets called by the runtime. Use this method to add services to the container.
       public void ConfigureServices(IServiceCollection services)
       {
           services.AddRazorPages();

           services.AddAuthentication(IISDefaults.AuthenticationScheme);

           services.AddTransient<IClaimsTransformation, CustomClaimsTransformer>();

           services.AddSingleton<IAuthorizationHandler, CheckGroupHandler>();

           var physicalProvider = _env.ContentRootFileProvider;
           var manifestEmbeddedProvider = 
               new ManifestEmbeddedFileProvider(typeof(Program).Assembly);
           var compositeProvider = 
               new CompositeFileProvider(physicalProvider, manifestEmbeddedProvider);

           services.AddSingleton<IFileProvider>(compositeProvider);

       }

       // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
       public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
       {
           if (env.IsDevelopment())
           {
               app.UseDeveloperExceptionPage();
           }
           else
           {
               app.UseExceptionHandler("/Error");
               // The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
               app.UseHsts();
           }

           app.UseHttpsRedirection();
           app.UseStaticFiles();

           app.UseRouting();

           app.UseAuthentication();
           app.UseAuthorization();

           app.UseEndpoints(endpoints => { endpoints.MapRazorPages(); });
       }
   }
}

我錯過了什麼?我嘗試安裝 NuGet 包,但沒有任何區別。

如果您從 ASP.NET-Core 2.x 遷移到 3.x,由於 ASP.NET-Core 3.0 及更高版本,Microsoft.NET.Sdk.WebMSBuild SDK 不再自動包含Microsoft.Extensions.FileProviders.EmbeddedNuGet 包。

Microsoft.Extensions.FileProviders.Embedded需要明確添加。

<Project Sdk="Microsoft.NET.Sdk.Web">
   ...
   <ItemGroup>
       <PackageReference Include="Microsoft.Extensions.FileProviders.Embedded" Version="3.0.3" />
       <!-- Or use version 3.1.2 for ASP.NET-Core 3.1 -->
   </ItemGroup>
   ...
</Project>

對於那些沒有從 2.x 遷移到 3.x 的人,不要忘記將以下內容添加到您的.csproj:

<Project Sdk="Microsoft.NET.Sdk.Web">
   ...
   <PropertyGroup>
       <GenerateEmbeddedFilesManifest>true</GenerateEmbeddedFilesManifest>
   </PropertyGroup>
   ...
   <ItemGroup>
       <EmbeddedResource Include="..." /> <!-- Add your directories and/or files here. -->
   </ItemGroup>
   ...
</Project>

引用自:https://stackoverflow.com/questions/59669223