2

我正在从 asp-net core 2.2 迁移到 asp-net core 3.0,我使用这个块来定义我的文件类提供程序,你知道这是用来创建动态剃须刀视图(动态 cshtml),我的问题是段确实不行。

我已经实现了经典:

services.Configure<RazorViewEngineOptions>(opts =>
                opts.FileProviders.Add( 
                    new MyCostumizedFileProvider()
                )
            );

在哪里:

  1. MyCostumizedFileProvider:是文件提供者的实现。

  2. RazorViewEngineOptions:是 aspnet core 2.0 中使用的 Razor 运行时编译器的处理程序。

在 asp-net core 中我检查过

像这样的例子:

services.Configure<MvcRazorRuntimeCompilationOptions>(options => {
    options.FileProviders.Clear();
    options.FileProviders.Add(new MyCostumizedFileProvider());
});

或者

  services.Configure<MvcRazorRuntimeCompilationOptions>(options => {
   options.FileProviders.Add(new MyCostumizedFileProvider());
  });

错误总是:

/Pages/Shared/ViewListPerson_1b2b2019-aad9-4096-a3b7-ece5dfe586c1.cshtml
   at Microsoft.AspNetCore.Mvc.ViewEngines.ViewEngineResult.EnsureSuccessful(IEnumerable`1 originalLocations)
   at Microsoft.AspNetCore.Mvc.ViewFeatures.ViewResultExecutor.ExecuteAsync(ActionContext context, ViewResult result)
   at Microsoft.AspNetCore.Mvc.ViewResult.ExecuteResultAsync(ActionContext context)
   at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.<InvokeNextResultFilterAsync>g__Awaited|29_0[TFilter,TFilterAsync](ResourceInvoker invoker, Task lastTask, State next, Scope scope, Object state, Boolean isCompleted)
   at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.Rethrow(ResultExecutedContextSealed context)
   at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.ResultNext[TFilter,TFilterAsync](State& next, Scope& scope, Object& state, Boolean& isCompleted)
   at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.InvokeResultFilters()
--- End of stack trace from previous location where exception was thrown ---
   at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.<InvokeNextResourceFilter>g__Awaited|24_0(ResourceInvoker invoker, Task lastTask, State next, Scope scope, Object state, Boolean isCompleted)
   at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.Rethrow(ResourceExecutedContextSealed context)
   at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.Next(State& next, Scope& scope, Object& state, Boolean& isCompleted)
   at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.InvokeFilterPipelineAsync()
--- End of stack trace from previous location where exception was thrown ---
   at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.<InvokeAsync>g__Logged|17_1(ResourceInvoker invoker)
   at Microsoft.AspNetCore.Builder.RouterMiddleware.Invoke(HttpContext httpContext)
   at Microsoft.AspNetCore.Builder.RouterMiddleware.Invoke(HttpContext httpContext)
   at Microsoft.AspNetCore.Session.SessionMiddleware.Invoke(HttpContext context)
   at Microsoft.AspNetCore.Session.SessionMiddleware.Invoke(HttpContext context)
   at Microsoft.AspNetCore.Diagnostics.DeveloperExceptionPageMiddleware.Invoke(HttpContext context)

你有线索,解决方案吗?或者你知道注入 FileProviders 的其他方法吗?

4

2 回答 2

7

对于 ASP.NET MVC Core 3,这是添加文件提供程序的方法:

services.AddControllersWithViews()
    .AddRazorRuntimeCompilation(options => options.FileProviders.Add(
        new PhysicalFileProvider(appDirectory)));
于 2019-10-08T15:47:01.247 回答
0

DavidG 为我们提供了解决方案:(我已经测试过并且有效)。

解决方案是:

services.AddControllersWithViews().AddRazorRuntimeCompilation(
    options => options.FileProviders.Add(new PhysicalFileProvider(appDirectory)));

在这种情况下:

services.AddControllersWithViews().AddRazorRuntimeCompilation(
    options => options.FileProviders.Add(new MyCostumizedFileProvider()));
于 2019-10-08T15:43:25.087 回答